我有以下查询:
ALTER procedure [dbo].[jk_insertAllLocation]
@locationTbl as locationTable readonly,
@TableName varchar(100)
as
declare @tbl as locationTable,@sql varchar(max)
begin
set @sql = 'insert into ' + @TableName +'(location_id,name,address,latitude,longitude,distance,state,sub_cat,id_cat,icon_link,checkinsCount,IDSearch)
select * from ' + @locationTbl
exec sp_executesql @sql
end
我需要传递一个表和一个表名作为参数,我需要插入表名(@TableName
)作为参数传递表中的所有数据(@locationTbl
)作为参数传递
但我知道我无法在查询中连接表(@locationTbl
)......
所以我该如何解决这个问题?
答案 0 :(得分:1)
您可以使用temp tables(链接上的临时表部分):
ALTER procedure [dbo].[jk_insertAllLocation]
@locationTbl as locationTable readonly,
@TableName varchar(100)
as
begin
declare @tbl as locationTable,@sql varchar(max)
if object_id('#_tmp_location_table') is not null drop table #_tmp_location_table
select * into #_tmp_location_table from @locationTbl
set @sql = 'insert into ' + @TableName + '(location_id,name,address,latitude,longitude,distance,state,sub_cat,id_cat,icon_link,checkinsCount,IDSearch) select * from #_tmp_location_table'
exec sp_executesql @sql
end