由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Quant版 - error of executing SQL query of string concatenation
相关主题
SQL check likeness between two large tables (转载)get value returned by SQLstored procedure from python (转载)
sort two same tables SQL but different results (转载)compare two large tables SQL (转载)
SQL copy a table into a new table and add a new column (转载)SQL select one value column for each distinct value another (转载)
SQL find distinct values in large table (转载)SQL combine two columns from two different tables no shared (转载)
SQL run a stored procedure by fetching from a cursor row by (转载)difference between a quant and programmer?
请教一道面试题啊!关于SQL的!大家来做一道题吧
SQL add some columns into a table from another table (转载SQL, investment firm recruiter发过来的面试题 (转载)
请教- 怎么对R code加密? (转载)C#, investment firm recruiter发过来的面试题 (转载)
相关话题的讨论汇总
话题: table话题: dbo话题: declare话题: temp话题: set
进入Quant版参与讨论
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页)
进入Quant版参与讨论
相关主题
C#, investment firm recruiter发过来的面试题 (转载)SQL run a stored procedure by fetching from a cursor row by (转载)
[合集] STL面试问题 (转载)请教一道面试题啊!关于SQL的!
A INTERVIEW PROBLEMSQL add some columns into a table from another table (转载
an interview question(finance)请教- 怎么对R code加密? (转载)
SQL check likeness between two large tables (转载)get value returned by SQLstored procedure from python (转载)
sort two same tables SQL but different results (转载)compare two large tables SQL (转载)
SQL copy a table into a new table and add a new column (转载)SQL select one value column for each distinct value another (转载)
SQL find distinct values in large table (转载)SQL combine two columns from two different tables no shared (转载)
相关话题的讨论汇总
话题: table话题: dbo话题: declare话题: temp话题: set