4个回答
展开全部
拼串的时候用char(39) 代替单引号,char(37)代替%
SET @sql = 'SELECT * FROM authors WHERE address LIKE '+char(39)+char(37)+@str+char(37)+char(39)
SET @sql = 'SELECT * FROM authors WHERE address LIKE '+char(39)+char(37)+@str+char(37)+char(39)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不要用双引号 只用单引号
SET @sql = 'SELECT * FROM authors WHERE address LIKE ''%'+@str+'%'''
SET @sql = 'SELECT * FROM authors WHERE address LIKE ''%'+@str+'%'''
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-11-02
展开全部
定义\为转义字符
SET @sql = 'SELECT * FROM authors WHERE address LIKE '\'%\'+@str+\'%\'' ESCAPE '\'
SET @sql = 'SELECT * FROM authors WHERE address LIKE '\'%\'+@str+\'%\'' ESCAPE '\'
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
带参数的不能这么调用。参考MSDN的sp_executesql
sp_executesql [ @stmt = ] stmt
[
{, [@params=] N'@parameter_name data_type [ OUT | OUTPUT ][,...n]' }
{, [ @param1 = ] 'value1' [ ,...n ] }
]
例:
DECLARE @IntVariable int;
DECLARE @SQLString nvarchar(500);
DECLARE @ParmDefinition nvarchar(500);
/* Build the SQL string one time.*/
SET @SQLString =
N'SELECT EmployeeID, NationalIDNumber, Title, ManagerID
FROM AdventureWorks.HumanResources.Employee
WHERE ManagerID = @ManagerID';
SET @ParmDefinition = N'@ManagerID tinyint';
/* Execute the string with the first parameter value. */
SET @IntVariable = 197;
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@ManagerID = @IntVariable;
/* Execute the same string with the second parameter value. */
SET @IntVariable = 109;
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@ManagerID = @IntVariable;
sp_executesql [ @stmt = ] stmt
[
{, [@params=] N'@parameter_name data_type [ OUT | OUTPUT ][,...n]' }
{, [ @param1 = ] 'value1' [ ,...n ] }
]
例:
DECLARE @IntVariable int;
DECLARE @SQLString nvarchar(500);
DECLARE @ParmDefinition nvarchar(500);
/* Build the SQL string one time.*/
SET @SQLString =
N'SELECT EmployeeID, NationalIDNumber, Title, ManagerID
FROM AdventureWorks.HumanResources.Employee
WHERE ManagerID = @ManagerID';
SET @ParmDefinition = N'@ManagerID tinyint';
/* Execute the string with the first parameter value. */
SET @IntVariable = 197;
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@ManagerID = @IntVariable;
/* Execute the same string with the second parameter value. */
SET @IntVariable = 109;
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@ManagerID = @IntVariable;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询