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* n *

谢谢

4

2 回答 2

1

如果您想像上面显示的那样显示。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");
            }
        }
      }
    }
于 2013-03-22T11:25:03.987 回答
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

然后将结果复制并粘贴到您需要的任何地方

于 2013-03-22T10:11:43.557 回答