久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 SQL Server 存儲過程中動態創建和選擇表變量?

Creating and Selecting table variables dynamically in SQL Server stored procedure?(在 SQL Server 存儲過程中動態創建和選擇表變量?)
本文介紹了在 SQL Server 存儲過程中動態創建和選擇表變量?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

請指導我如何動態創建表變量.我知道它可以是這樣的:

Please guide me how to create table variables dynamically. I know it can be like this:

DECLARE @people TABLE 
( 
    id INT, 
    name VARCHAR(32) 
);

如果不知道列和數據類型,我將如何創建.場景是我必須根據物理表創建表變量并將數據選擇到其中,在 SP 中使用它們,然后將表變量中的數據返回到 C# 程序.

How I will create if dont know columns and data types. Scenario is I have to create table variable as per a physical tables and selct data into them, use them in SP and then return data in table variables to C# program.

例如我有一張員工表.我想創建一個與 Employyes 具有相同結構的表變量.但是提到列需要很多時間(因為員工大約有 100 列)

For example I have a table Employees. I want create a table variable with same structure as Employyes have. But mentioning columns take a lot time (as Employees have about 100 columns)

請指教.

推薦答案

這里是如何動態構建 DECLARE TABLE 語句的方法:

So here is how you can build the DECLARE TABLE statement dynamically:

DECLARE @sql NVARCHAR(MAX);
SET @sql = N'';


SELECT @sql = @sql + ',
' + c.name + ' ' + t.name
 + CASE
  WHEN t.name LIKE '%char' OR t.name LIKE '%binary' THEN
   '(' + CASE WHEN c.max_length = -1 THEN 'MAX' ELSE
   CONVERT(VARCHAR(4), c.max_length/CASE WHEN t.name LIKE 'n%'
   THEN 2 ELSE 1 END) END + ')'
  WHEN t.name IN ('float') THEN
      '(' + CONVERT(VARCHAR(4), c.precision) + ')'
  WHEN t.name IN ('decimal', 'numeric') THEN
   '(' + CONVERT(VARCHAR(4), c.precision) + ','
   + CONVERT(VARCHAR(4), c.scale) + ')'
  ELSE ''
 END
 FROM sys.columns AS c
 INNER JOIN sys.types AS t
 ON c.system_type_id = t.system_type_id
 AND c.user_type_id = t.user_type_id
 WHERE c.[object_id] = OBJECT_ID('dbo.Employees')
 ORDER BY c.column_id;

SET @sql = 'DECLARE @people TABLE (' + STUFF(@sql, 1, 1, '') + '
);';

SELECT @sql;

但是現在呢?你不能從動態 SQL 的范圍之外插入它:

But now what? You can't insert into it from outside the scope of dynamic SQL:

EXEC sp_executesql @sql;
INSERT @people(id, name) SELECT 1,'foo';

產生此錯誤:

Msg 1087, Level 15, State 2, ...
Must declare the table variable "@people".

再一次,一個范圍問題 - @people 只存在于動態 SQL 中,一旦完成就不再存在.因此,雖然您可以繼續并附加到 @sql 變量:

Once again, a scoping issue - @people only exists in the dynamic SQL and ceases to exist as soon as it's finished. So while you could proceed and append to the @sql variable:

SET @sql = @sql + 'INSERT @people ...; SELECT id, name FROM @people;';

...這會很快失控.而且我仍然不知道您的 C# 代碼如何知道所涉及的所有列和數據類型,但是編寫 SQL 來做到這一點太難了……您知道可以將列節點從對象資源管理器拖到查詢上窗口,它會為您生成一個很好的逗號分隔的列名列表,對嗎?

...this will get out of hand very quickly. And I still have no idea how your C# code can be aware of all the columns and data types involved, but it's too hard to write the SQL to do so... you know you can drag the columns node from Object Explorer onto a query window and it will produce a nice comma-separated list of column names for you, right?

這篇關于在 SQL Server 存儲過程中動態創建和選擇表變量?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

SQL - Select all when filter value is empty(SQL - 過濾器值為空時全選)
How and where do I set the MAXRECURSION option?(如何以及在何處設置 MAXRECURSION 選項?)
TABLOCKX versus SERIALIZABLE(TABLOCKX 與可序列化)
TSQL Constants... Use Variable or Literal?(TSQL 常量...使用變量還是文字?)
TSQL RIGHT String function not working(TSQL RIGHT 字符串函數不起作用)
What is the comparative speed of temporary tables to physical tables in SQL?(SQL中臨時表與物理表的比較速度是多少?)
主站蜘蛛池模板: 亚洲精品中文字幕中文字幕 | 91影院在线观看 | 国产精品久久久久久妇女6080 | 成人福利电影 | 国产精品99 | 国产精品视频久久久 | 国产精品免费大片 | 久久精品二区 | 午夜网| 91久久国产综合久久 | 久久久久久久一区 | 国产精品国产三级国产aⅴ中文 | 亚洲欧美日韩一区二区 | 欧美日韩精品在线免费观看 | 国产精品久久国产精品99 | 黑人一级黄色大片 | 日韩影院一区 | 国产精品一区在线观看 | h肉视频| 欧美午夜精品理论片a级按摩 | 色婷婷国产精品综合在线观看 | 国产做a爱片久久毛片 | 久久久久成人精品 | 久久91精品国产一区二区三区 | 成人久久18免费网站图片 | 丁香久久 | 亚洲国产高清高潮精品美女 | 成人一级片在线观看 | 中文字幕一区二区在线观看 | 美国十次成人欧美色导视频 | 久久久亚洲精品视频 | 91久久北条麻妃一区二区三区 | 亚洲一级二级三级 | 亚洲欧洲国产视频 | 美日韩精品| 精品国产不卡一区二区三区 | 中文字幕日韩欧美 | 国产一区久久 | 日韩电影中文字幕 | 欧美综合久久久 | 亚洲欧美视频一区 |