2011年3月7日 星期一

if .. else .. create table variable

想要動依不同情況建立相同名稱的table variable,語法如下
 if @condition = '1'
begin
  declare @tmptable1 table (col1 int,col2 int)
end
else
begin
  declare @tmptable1 table (col1 int,col2 int, col3 int, col4 int)
end

出現以下錯誤
Msg 134, Level 15, State 1, Line 13
The variable name '@tmptable1' has already been declared. Variable names must be unique within a query batch or stored procedure.

改成用temp table 也不行
 if @condition = '1'
begin
    create table #tmptable1  (col1 int,col2 int)
end
else
begin
   create table #tmptable1  (col1 int,col2 int, col3 int, col4 int)
end


 出現以下錯誤
Msg 2714, Level 16, State 1, Line 15
There is already an object named '#tmptable1' in the database.

最後折衷好了...馬是口以啦...

 create table #tmptable1  (col1 int,col2 int, col3 int, col4 int)
 if @condition = '1'
begin
    alter table #tmptable1
    drop column col3,col4
end

 

沒有留言:

張貼留言

IIS 網站無法下載中文檔名檔案因為取消高位元字元

 網址+中文檔案名稱方式下載檔案顯示 404 - 找不到檔案或目錄。 但英數字檔案名稱則可下載。 因為設定GCB IIS,其中1項 將高位元字元預設取消勾選了。 27 TWGCB-04-014-0028 要求篩選與其他限制模組 允許高位元字元 這項原則設定決定查詢...