2017-06-01 57 views
0

我需要獲取表名作爲輸出,下面是示例代碼。使用存儲過程作爲輸出的SQL顯示錶

eg. set @table = [dbo].[mytable]

 Insert into <table 1> (a, b, c, d as table name, i) 
    select a, b , c, '[email protected]+' , i from <table 2> 
    inner join <table2> on a = tmp.a ` 

在INSERT語句中,我需要列d擁有適用於所有記錄的表名。我收到一個錯誤「多部分不能被綁定」是否有一種特殊的方式來調用表名稱來顯示唯一的目的?

謝謝!

+1

你的樣品查詢根本沒有意義。什麼是'tmp'?爲什麼自我加入? –

回答

0

首先,你要插入文本,所以把它放在引號

set @table = '[dbo].[mytable]' 

其次,別名你的表,並適用於列(你有兩個表2)

Insert into [Table1] (a, b, c, d, i) 
select t1.a, t1.b , t1.c, @table , t1.i 
from [Table2] t1 
inner join [Table2] t2 on t1.a = t2.a 
相關問題