本文介紹了動態添加 50 列到表變量的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想向定義為變量的表中添加大約 50 列.這些列的名稱來自另一個表,基本上它們只是數字 - DEPA_KEY.(部門鍵)
I would like to add around 50 columns to table defined as variable. Names of those columns are coming from another table and basically they are just numbers - DEPA_KEY. (department key)
是否可以使用循環或類似的東西動態添加這些列?
Is it possible to add those columns dynamically with loop or something similar?
表定義為變量(這里我想動態添加 50 列):
Table defined as variable (here I would like dynamically add 50 columns):
DECLARE @USERS TABLE
(
USER_KEY INT,
USDE_HSU DECIMAL(8,2)
)
查詢所有部門:
SELECT DEPA_KEY FROM CADEPA
推薦答案
AFAIK,你不能改變表變量的結構:
AFAIK, You can't change the structure of a table variable:
DECLARE @T AS TABLE
(
col1 int
);
ALTER TABLE @T
ADD col2 char(1)
;
這會產生一個錯誤.
您可以,但是,使用臨時表來做到這一點:
You can, however, do it with a temporary table:
CREATE TABLE #T
(
col1 int
);
ALTER TABLE #T
ADD col2 char(1)
;
這篇關于動態添加 50 列到表變量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!