executeSql的问题

yamyuyang 2003-07-15 09:24:35
sql statement is as followed:
----------------------------------------
declare @tableName VarChar(20)
declare @id VarChar(2)
declare @csName VarChar(30)
set @tableName='js_des'
set @id='1'
declare @strSql varchar(200)
set @strSql='select @csName=count(*) from '+tableName+' where id='+@id
exec sp_executesql @strSql,N'@csName varchar(30) output',@csName output
select @strSql
erro:过程需要参数 '@statement' 为 'ntext/nchar/nvarchar' 类型。
...全文
24 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yamyuyang 2003-07-15
  • 打赏
  • 举报
回复
3x for all.
zjcxc(邹建) 25
pengdali(大力 V2.0) :15
becaust zjcxc(邹建) 25 tell me the answer:the type of the first and second parameter must be unicode


pengdali 2003-07-15
  • 打赏
  • 举报
回复
declare @strSql nvarchar(200)
zjcxc 2003-07-15
  • 打赏
  • 举报
回复
sp_executesql
执行可以多次重用或动态生成的 Transact-SQL 语句或批处理。Transact-SQL 语句或批处理可以包含嵌入参数。

语法
sp_executesql [@stmt =] stmt
[
{, [@params =] N'@parameter_name data_type [,...n]' }
{, [@param1 =] 'value1' [,...n] }
]

参数
[@stmt =] stmt

包含 Transact-SQL 语句或批处理的 Unicode 字符串,stmt 必须是可以隐式转换为 ntext 的 Unicode 常量或变量。不允许使用更复杂的 Unicode 表达式(例如使用 + 运算符串联两个字符串)。不允许使用字符常量。如果指定常量,则必须使用 N 作为前缀。例如,Unicode 常量 N'sp_who' 是有效的,但是字符常量 'sp_who' 则无效。字符串的大小仅受可用数据库服务器内存限制。

stmt 可以包含与变量名形式相同的参数,例如:

N'SELECT * FROM Employees WHERE EmployeeID = @IDParameter'

stmt 中包含的每个参数在 @params 参数定义列表和参数值列表中均必须有对应项。

[@params =] N'@parameter_name data_type [,...n]'

字符串,其中包含已嵌入到 stmt 中的所有参数的定义。该字符串必须是可以隐式转换为 ntext 的 Unicode 常量或变量。每个参数定义均由参数名和数据类型组成。n 是表明附加参数定义的占位符。stmt 中指定的每个参数都必须在 @params 中定义。如果 stmt 中的 Transact-SQL 语句或批处理不包含参数,则不需要 @params。该参数的默认值为 NULL。
pengdali 2003-07-15
  • 打赏
  • 举报
回复
declare @tableName VarChar(20)
declare @id VarChar(2)
declare @csName VarChar(30)
set @tableName='js_des'
set @id='1'
declare @strSql nvarchar(200)
set @strSql='select @csName=count(*) from '+tableName+' where id='+@id
exec sp_executesql @strSql,N'@csName varchar(30) output',@csName output
select @strSql
zjcxc 2003-07-15
  • 打赏
  • 举报
回复
有两处错误:
declare @tableName VarChar(20)
declare @id VarChar(2)
declare @csName VarChar(30)
set @tableName='js_des'
set @id='1'
declare @strSql Nvarchar(200) --sp_executesql的第一二个参数的类型必须是unicode类型
set @strSql='select @csName=count(*) from '+@tableName+' where id='+@id
--上面的这句,你原来写的是 tableName ,实际应该为 @tableName
exec sp_executesql @strSql,N'@csName varchar(30) output',@csName output
select @strSql
yamyuyang 2003-07-15
  • 打赏
  • 举报
回复
please check it:0
yamyuyang 2003-07-15
  • 打赏
  • 举报
回复
i modify it as followed:
set @strSql=N'select @csName=count(*) from '+@tableName+' where id='+@id
but the erro is the same.
yamyuyang 2003-07-15
  • 打赏
  • 举报
回复
please tell me the answer and 3x
friendliu 2003-07-15
  • 打赏
  • 举报
回复
首先指出你一个错误
set @strSql='select @csName=count(*) from '+tableName+' where id='+@id

+tableName+
这里应该是+@tablename+
愉快的登山者 2003-07-15
  • 打赏
  • 举报
回复
set @strSql=N'select @csName=count(*) from '+tableName+' where id='+@id

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧