create table tb(col varchar(10)) insert into tb values('测试') insert into tb values('test') goexec sp_executesql N'select * from tb where col=''测试''' /* col ---------- 测试(所影响的行数为 1 行) */exec sp_executesql N'select * from tb where col=''test''' /* col ---------- test(所影响的行数为 1 行) */drop table tb
楼主参考一下动态SQL的基本语法.--动态sql语句基本语法
1 :普通SQL语句可以用Exec执行 eg: Select * from tableName Exec('select * from tableName') Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N 2:字段名,表名,数据库名之类作为变量时,必须用动态SQL eg: declare @fname varchar(20) set @fname = 'FiledName' Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。 Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格 当然将字符串改成变量的形式也可 declare @fname varchar(20) set @fname = 'FiledName' --设置字段名 declare @s varchar(1000) set @s = 'select ' + @fname + ' from tableName' Exec(@s) -- 成功 exec sp_executesql @s -- 此句会报错 declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000) set @s = 'select ' + @fname + ' from tableName' Exec(@s) -- 成功 exec sp_executesql @s -- 此句正确 3. 输出参数 declare @num int, @sqls nvarchar(4000) set @sqls='select count(*) from tableName' exec(@sqls) --如何将exec执行结果放入变量中? declare @num int, @sqls nvarchar(4000) set @sqls='select @a=count(*) from tableName ' exec sp_executesql @sqls,N'@a int output',@num output select @num
为什么如果字段名不是中文的话exec sp_executesql N'select * from tb where col=test'这样写也能执行成功呢 但换成中文了就必须象楼上朋友说的那样加单引号转义呢/?能解释下嘛?
英文应该也不行的吧?exec sp_executesql N'select * from tb where col=test'/**服务器: 消息 207,级别 16,状态 3,行 1 列名 'test' 无效。**/
为什么如果字段名不是中文的话exec sp_executesql N'select * from tb where col=test'这样写也能执行成功呢 但换成中文了就必须象楼上朋友说的那样加单引号转义呢/?能解释下嘛? 应该也是要加''的,我测试中,不加''报错
exec sp_executesql N'select * from tb where col=test'你这句话能行?不太可能吧?你col是字符串型的吧,所以得用引号.如果是中文最好还加个N.exec sp_executesql N'select * from tb where col=N''测试'''
执行动态语句,如果有错误的话,你把动态语句打印出来单独运行一下 看能不能行得通,问题自然就明了了。select * from student where stu_id = 09010101 select * from student where stu_id = '09010101' 都正常运行 select * from student where stu_name = '张三' 正常 select * from student where stu_name = 张三 /* Msg 207, Level 16, State 1, Line 1 Invalid column name '张三'. */
执行动态语句,如果有错误的话,你把动态语句打印出来单独运行一下 看能不能行得通,问题自然就明了了。参考一下 select * from student where stu_id = 09010101 select * from student where stu_id = '09010101' 都正常运行 select * from student where stu_name = '张三' 正常 select * from student where stu_name = 张三 /* Msg 207, Level 16, State 1, Line 1 Invalid column name '张三'. */
exec sp_executesql N'select * from users where addr="测试"'对于中文加双引号即可
insert into tb values('测试')
insert into tb values('test')
goexec sp_executesql N'select * from tb where col=''测试'''
/*
col
----------
测试(所影响的行数为 1 行)
*/exec sp_executesql N'select * from tb where col=''test'''
/*
col
----------
test(所影响的行数为 1 行)
*/drop table tb
1 :普通SQL语句可以用Exec执行 eg: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N 2:字段名,表名,数据库名之类作为变量时,必须用动态SQL eg:
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格 当然将字符串改成变量的形式也可
declare @fname varchar(20)
set @fname = 'FiledName' --设置字段名 declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句会报错 declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句正确 3. 输出参数
declare @num int,
@sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--如何将exec执行结果放入变量中? declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num
但换成中文了就必须象楼上朋友说的那样加单引号转义呢/?能解释下嘛?
英文应该也不行的吧?exec sp_executesql N'select * from tb where col=test'/**服务器: 消息 207,级别 16,状态 3,行 1
列名 'test' 无效。**/
但换成中文了就必须象楼上朋友说的那样加单引号转义呢/?能解释下嘛? 应该也是要加''的,我测试中,不加''报错
exec sp_executesql N'select * from tb where col=test'你这句话能行?不太可能吧?你col是字符串型的吧,所以得用引号.如果是中文最好还加个N.exec sp_executesql N'select * from tb where col=N''测试'''
看能不能行得通,问题自然就明了了。select * from student where stu_id = 09010101
select * from student where stu_id = '09010101'
都正常运行
select * from student where stu_name = '张三'
正常
select * from student where stu_name = 张三
/*
Msg 207, Level 16, State 1, Line 1
Invalid column name '张三'.
*/
看能不能行得通,问题自然就明了了。参考一下
select * from student where stu_id = 09010101
select * from student where stu_id = '09010101'
都正常运行
select * from student where stu_name = '张三'
正常
select * from student where stu_name = 张三
/*
Msg 207, Level 16, State 1, Line 1
Invalid column name '张三'.
*/