0

我有一个 sql 查询,它从 SQLdatabase 检索信息,我需要为发送给客户端的每个批次添加前导和尾随。我已经搜索过,但不走运。标题记录应该有 Recordtype(1)CPC 编号 (0000) 文件序列号 (001) 并且尾部记录应该有记录类型 (1) 请求总数 (5)、总诉讼费 ()、总索赔金额 ()。

      string stringSql1 = " SELECT distinct  " +
             "'" + comboBox6.Text + "' as Recordtype" +
             ",'" + textBox5.Text + "' as Recordtype" +

               " , space(1983) ";

          string stringSql = " SELECT distinct  " +
            "'" + comboBox6.Text + "' as RecordType" +
             " , left([Claimant Name] +'                              ',30) " +
             " , left([Claimant Address1] +'                             ',30)  " +
              " , left([Claimant Address2] +'                             ',30) as ClaimantAddress2 " +
              " , left([Claimant Address3] +'                             ',30) as ClaimantAddress3 " +
              " , left([Claimant Address4] +'                             ',30) as ClaimantAddress4 " +
    string whereClause = "";

===so on ....==================================

               string[] tempArray = new string[this.txt.Lines.Length];
                tempArray = this.txt.Lines;

                if (this.txt.Lines.Length == 0)
                {
                    return;
                }

                for (int counter = 0; counter <= tempArray.Length-1; counter++)
                {
                    if (tempArray[counter].Trim().Length > 0)
                    {
                        whereClause = whereClause + "'" + tempArray[counter] + "'" + ", ";
                    }
                }
                whereClause = whereClause.TrimEnd(' ', ',');
                whereClause = "(" + whereClause + ")";

                stringSql = stringSql.Replace("{where}", whereClause);

                myDataset = new DataSet("SQL");
                SqlConnection myConn = new SqlConnection();


                SqlCommand myCommand = new SqlCommand();
                myCommand.CommandType = CommandType.Text;
                myCommand.CommandText = stringSql;

                myCommand.Connection = myConn;

                SqlDataAdapter myAdapter = new SqlDataAdapter();
                myAdapter.SelectCommand = myCommand;
                myAdapter.Fill(myDataset);
4

1 回答 1

0

假设您希望在单个SELECT语句中完成所有这些,您可以将包含汇总信息(如总计、统计信息和内容)的列添加到典型的SELECT. 例如

  declare @sumTotal decimal
  set @sumTotal = (SELECT SUM(ColumnName) FROM tableNameY)
--declare similar variables for total number of request(5), total court fee() and total claim amount(). 

--Create a query that outputs row data along with the info you just calculated

  select '0000' as 'CPC number', '001' as 'File sequence number', ColumnName, @sumTotal as 'total number of request', @totalCourtFeesvariable as 'total court fees'--etc...
  from tableNameX
于 2013-10-17T15:25:49.023 回答