2013-03-22 57 views
0

這裏下面是MT數據庫表如何查詢,從而獲得所需的數據

CREATE TABLE [dbo].[mtblLog_Book](
[Logbook_Number] [int] IDENTITY(1,1) NOT NULL, 
[Sno] [int] NULL, 
[Vehicle_Number] [nvarchar](100) NULL, 
[Vehicle_Booking_Date] [datetime] NULL, 
[Time_From] [datetime] NULL, 
[Time_To] [datetime] NULL, 
[KM_Start] [int] NULL, 
[KM_End] [int] NULL, 
[Total] [int] NULL, 
[Vehicle_Used_By] [varchar](100) NULL, 
[Cost_Code] [nvarchar](50) NULL, 
[Budget_Line] [nvarchar](50) NULL, 
[Entry_Date] [datetime] NULL 
) ON [PRIMARY] 

有反覆Cost_Code。我需要一個查詢,這樣我可以在下面的格式獲得數據

CostCode1 
...................... 
All data which belong to CostCode1 
...................... 

CostCode2 
...................... 
All data which belong to CostCode2 
...................... 

CostCode3 
...................... 
All data which belong to CostCode3 
...................... 

續....到CostCode * ň *

感謝

+0

你不能在兩者之間打印出頭條新聞的方式。在Sql中,每個查詢結果必須具有相同數量的顏色。那麼'SELECT * FROM dbo.mtblLog_Book ORDER BY Cost_Code' – JaMaBing 2013-03-22 09:37:33

回答

0

如果您正在使用SQL Server和SSMS,你可以將其設置爲輸出結果爲文本,不喜歡

DECLARE @costCode NVARCHAR(50) 

DECLARE cur CURSOR FOR 
SELECT DISTINCT Cost_Code 
FROM dbo.mtblLog_Book 

OPEN cur 
FETCH NEXT FROM cur INTO @costCode 
WHILE @@FETCH_STATUS=0 
BEGIN 
    PRINT @costCode 
    PRINT '......................................' 
    SELECT * FROM dbo.mtblLog_Book 
    WHERE Cost_Code = @costCode 
    PRINT '......................................' 
       PRINT '' 
END 

CLOSE cur 
DEALLOCATE cur 

然後複製的東西,結果貼在任何需要它

+0

怎麼辦?它不能停止。它運行到無窮大 – Gaurav 2013-04-01 04:12:44

1

如果你想顯示你一樣表現出above.thn使用此代碼

protected void show() 
    { 
    Response.Clear(); 
    string sql = "select Cost_Code from mtblLog_Book"; 
    ds = obj.openDataset(sql, schoolCode); 

    if (ds.Tables[0].Rows.Count == 0) 
    { 
     Response.Write("[{\"Records\":\"" + "NA" + "\"}]"); 
    } 
    else 
    { 
     int i; 
     string output; 
     for (i = 0; i < ds.Tables[0].Rows.Count - 1; i++) 
     { 
     string sqlOutput ="Select * from mtblLog_Book where CostCode='"+ds.Tables[0].Rows[i][ "CostCode"].ToString()+ "';"; 
      Dataset dsOutPut= new Dataset; 
      output = "[{\" Logbook_Number \":\"" +dsOutPut.Tables[0].Rows[i]["Logbook_Number"].ToString() + "\",\" Sno\": \"" + dsOutPut.Tables[0].Rows[i][" Sno"] + "\",\" Vehicle_Number\": \"" + dsOutPut.Tables[0].Rows[i][" Vehicle_Number"].ToString() + "\",\" Vehicle_Booking_Date\": \"" + dsOutPut.Tables[0].Rows[i][" Vehicle_Booking_Date"].ToString() + "\" }]" 

      Response.Write(output); 
      if (i < dsOutPut.Tables[0].Rows.Count - 1) 
      { 
       Response.Write("\n"); 
      } 
     } 
     } 
    } 
+1

謝謝!它也適用於我,做得好:) – 2013-03-22 11:33:18

相關問題