由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - error of executing SQL query of string concatenation (转载
相关主题
请教set和select 的区别SQL combine two tables into one table and add a new column
求助:如何ColumName 作为变量放入querySQL copy a table into a new table and add a new column
问一个SQL Server的问题新手请教SQL 语法问题- alias 和 join
问个 sp_send_dbmail 的问题T-SQL 问题
紧急求助, 关于SQL ServerMS T-SQL 问题
SQL请教...怎么写这样的distribution list请教2个sql query 问题
请求SQL语句有包子!sql procedure 来rank不同table里面的数据
问个SQL问题- partial outer joinCommon Table Expression 问题
相关话题的讨论汇总
话题: table话题: dbo话题: declare话题: temp
进入Database版参与讨论
1 (共1页)
l******9
发帖数: 579
1
I am designing a SQL Server 2008 R2 query.
If I used string concatenation to insert into table, it does not work.
DECLARE @s1 varchar(MAX);
DECLARE @s2 varchar(MAX);
DECLARE @s3 varchar(MAX);
DECLARE @s4 varchar(MAX);
SET @s1 = 'SELECT a.id, b.name as new_name, a.value FROM ['
SET @s2 = '].[dbo].[table1] as a, '
SET @s3 = 'a_temp_table as b ' -- a_temp_table is a table variable. No
matter I put "@" or "#" in front of a_temp_table, it doe snot work.
SET @s4 = 'WHERE a.id = b.id and a.address = b.address '
INSERT INTO [dbo].[table2] **nothing is inserted**
EXEC(@s1 + @my_database_name + @s2 + @s3 + @s4) **this query return
nothing**
I need to access different databases at each iteration in a loop so I prefer
string concatenation.
This is the output from "print all string "
INSERT INTO [dbo].[table2]
SELECT a.id, b.name as new_name, a.value
FROM [@my_database_name].[dbo].[table1] as a, a_temp_table as b
WHERE a.id = b.id and a.address = b.address
It works if I change it to :
INSERT INTO [dbo].[table2]
SELECT a.id, b.name as new_name, a.value
FROM [@my_database_name].[dbo].[table1] as a, @a_temp_table as b
WHERE a.id = b.id and a.address = b.address
But, in string format, I got error:
Must declare the table variable "@a_temp_table".
1 (共1页)
进入Database版参与讨论
相关主题
Common Table Expression 问题紧急求助, 关于SQL Server
SQL question...SQL请教...怎么写这样的distribution list
SQL question: update a field请求SQL语句
怎样快速得到两个表的交集问个SQL问题- partial outer join
请教set和select 的区别SQL combine two tables into one table and add a new column
求助:如何ColumName 作为变量放入querySQL copy a table into a new table and add a new column
问一个SQL Server的问题新手请教SQL 语法问题- alias 和 join
问个 sp_send_dbmail 的问题T-SQL 问题
相关话题的讨论汇总
话题: table话题: dbo话题: declare话题: temp