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

 

沒有留言:

張貼留言

pdf.js 無法顯示部份字

有個檔案在pdf viewer套件中無法顯示內容,但下載檔案後使用工具又可以正常顯示。 本來以為是套件版本太舊的原因,於是去下載pdf viewer套件 https://github.com/mozilla/pdf.js 更新後還是一樣。 覺得應是字型缺漏的問題,於是用PDF-X...