2017-12-18 467 views
6

我想根據我找到的元素得到一個動態的結果集。這裏是我的查詢的一個示例:如何獲得從sql動態數據透視到vb

declare @til DateTime = dateadd(MINUTE, -0, getdate()) 
declare @fra datetime = DATEADD(MINUTE, -350, @til) 

declare @title nvarchar(max) = 'test title' 
DECLARE @cols AS NVARCHAR(MAX), 
@query AS NVARCHAR(MAX); 

create table errors (collection_id bigint, nr smallint, position smallint, stamp datetime) 
create table t (collection_id bigint, collection_name nvarchar(max), nr smallint, [status] smallint, stamp datetime) 

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(errors.position) 
         from t t 
         left join errors on errors.collection_id = t.collection_id and errors.nr = t.nr 
         where t.Status = 4 and errors.Stamp > @fra and t.collection_name = ''' + @title + ''' and errors.collection_id is not null 
         FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') , 1, 1, ''); 

select @cols 

set @query = 'declare @til DateTime = dateadd(MINUTE, -0, getdate()) 
      declare @fra datetime = DATEADD(MINUTE, -350, @til) 

      ;with cte as (select t.collection_name, errors.position, count(errors.Tryksag_Nedtagsfejl_Id) antal 
      from t 
      left join errors on errors.collection_id = s.collection_id and errors.nr = t.nr 
      where t.Status = 4 and errors.Stamp > @fra and and t.collection_name = ''' + @title + ''' 
      group by t.collection_name, errors.position) 

       SELECT collection_name, ' + @cols + ' from 
       cte 
      pivot 
      (
       sum(antal) 
       for position in (' + @cols + ') 
      ) p ' 

execute(@query) 

到目前爲止,我已經做了查詢,我可以在SSMS運行,並給出了輸出我的願望。這將是這樣的:

enter image description here

我怎樣才能使這個結果集提供給我在vb.net?當我運行它作爲查詢它不給我的結果(假設它不會看到從執行結果集),我認爲這是更好地創建一個存儲

添加VB代碼

Dim var_til As Short = 0 
      Dim var_fra As Short = -60 
      Dim Linie As String = "Red" 
      Dim tx = "Test title" 
      Dim Stt2 = "declare @til DateTime = dateadd(MINUTE, " & var_til & ", getdate()) " _ 
       & "declare @fra datetime = DATEADD(MINUTE, " & var_fra & " , @til) " _ 
       & "DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX) " _ 
       & "declare @linie as nvarchar(max) = '" & Linie & "' " _ 
       & "declare @title as nvarchar(max) = '" & tx & "' " _ 
       & "select @cols = STUFF((SELECT distinct ',' + QUOTENAME(errors.position) " _ 
       & "from t " _ 
       & "left join errors on errors.collection_id = t.collection_id And errors.nr = t.nr " _ 
       & "where t.Status = 4 And errors.Stamp > @fra And t.collection_name = ''' + @title + ''' and errors.collection_id is not null " _ 
       & "FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') , 1, 1, '') " _ 
       & " " _ 
       & "set @query = ';with cte as (select t.collection_name, errors.position, count(errors.Tryksag_Nedtagsfejl_Id) antal " _ 
       & "from t " _ 
       & "left join errors on errors.collection_id = s.collection_id And errors.nr = t.nr " _ 
       & "where t.Status = 4 And errors.Stamp > @fra And And t.collection_name = ''' + @title + ''' " _ 
       & "group by t.collection_name, errors.position) " _ 
       & "SELECT collection_name, ' + @cols + ' from " _ 
       & "cte " _ 
       & "pivot " _ 
       & "(" _ 
       & "sum(antal) " _ 
       & "for position in (' + @cols + ') " _ 
       & ") p ' " _ 
       & "execute(@query) " 

      Dim sqlConnection2 As New SqlConnection("Data Source=CONDOR-TI;Initial Catalog=Condor_db;Integrated Security=True") 
      Dim cmd2 As New SqlCommand 
      Dim reader2 As SqlDataReader 
      cmd2.CommandText = Stt2 
      cmd2.CommandType = CommandType.Text 
      cmd2.Connection = sqlConnection2 
      sqlConnection2.Open() 
      reader2 = cmd2.ExecuteReader() 
      While reader2.Read 
       Console.Write(reader2(0)) 
      End While 
      Console.WriteLine() 
      sqlConnection2.Close() 
      reader2.Close() 
+2

什麼是您的VB代碼是什麼樣子? – SMM

+0

[mcve] please:桌子和東西... – Blag

+0

如何在VB代碼中運行此查詢? – FLICKER

回答

4

過程來執行該SQL命令,並使用下面的代碼從VB.Net執行它:

Dim sqlConnection1 As New SqlConnection("Your Connection String") 
Dim cmd As New SqlCommand 
Dim reader As SqlDataReader 

cmd.CommandText = "StoredProcedureName" 
cmd.CommandType = CommandType.StoredProcedure 
cmd.Connection = sqlConnection1 

sqlConnection1.Open() 

reader = cmd.ExecuteReader() 
' Data is accessible through the DataReader object here. 

sqlConnection1.Close() 

參考

3

您可以分兩步運行。確保你在同一個連接上運行這兩個命令。

在第一個命令中,使用ExecuteNonQuery將cte選擇到#temp表中。

在第二個命令調用ExecuteReaer方法()

Dim var_til As Short = 0 
Dim var_fra As Short = -60 
Dim Linie As String = "Red" 
Dim tx = "Test title" 
Dim Stt2 = "declare @til DateTime = dateadd(MINUTE, " & var_til & ", getdate()) " _ 
    & "declare @fra datetime = DATEADD(MINUTE, " & var_fra & " , @til) " _ 
    & "DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX) " _ 
    & "declare @linie as nvarchar(max) = '" & Linie & "' " _ 
    & "declare @title as nvarchar(max) = '" & tx & "' " _ 
    & "select @cols = STUFF((SELECT distinct ',' + QUOTENAME(errors.position) " _ 
    & "from t " _ 
    & "left join errors on errors.collection_id = t.collection_id And errors.nr = t.nr " _ 
    & "where t.Status = 4 And errors.Stamp > @fra And t.collection_name = ''' + @title + ''' and errors.collection_id is not null " _ 
    & "FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') , 1, 1, '') " _ 
    & " " _ 
    & "set @query = ';with cte as (select t.collection_name, errors.position, count(errors.Tryksag_Nedtagsfejl_Id) antal " _ 
    & "from t " _ 
    & "left join errors on errors.collection_id = s.collection_id And errors.nr = t.nr " _ 
    & "where t.Status = 4 And errors.Stamp > @fra And And t.collection_name = ''' + @title + ''' " _ 
    & "group by t.collection_name, errors.position) " _ 
    & "SELECT collection_name, ' + @cols + ' INTO #OUT from " _ 
    & "cte " _ 
    & "pivot " _ 
    & "(" _ 
    & "sum(antal) " _ 
    & "for position in (' + @cols + ') " _ 
    & ") p ' " _ 
    & "execute(@query) " 

dim stt3 as string = "select * from #OUT" 

Dim sqlConnection2 As New SqlConnection("Data Source=CONDOR-TI;Initial Catalog=Condor_db;Integrated Security=True") 
Dim cmd2 As New SqlCommand 
cmd2.CommandText = Stt2 
cmd2.CommandType = CommandType.Text 
cmd2.Connection = sqlConnection2 

Dim cmd3 As New SqlCommand 
Dim reader3 As SqlDataReader 
cmd3.CommandText = Stt3 
cmd3.CommandType = CommandType.Text 
cmd3.Connection = sqlConnection2 

sqlConnection2.Open() 
cmd2.ExecuteNonQuery() 
reader3 = cmd3.ExecuteReader() 
While reader3.Read 
    Console.Write(reader3(0)) 
End While 
Console.WriteLine() 
sqlConnection2.Close() 
reader3.Close() 
+0

在應用程序代碼中發佈sql命令並不是一個好主意,我認爲使用存儲過程或表值函數 – Yahfoufi

+0

我同意。特別是在像VB這樣的面向行的語言中。至少在C#中,所有這些SQL都可以在單個字符串塊中。我避免像這樣嵌入SQL。我認爲這種方法一定還有其他原因。 – suresubs

相關問題