sql存储过程怎么使用变量来替代列名,比如select * from Table where @c
sql存储过程怎么使用变量来替代列名,比如select*fromTablewhere@col=@KeyWord...
sql存储过程怎么使用变量来替代列名,比如select * from Table where @col =@KeyWord
展开
展开全部
那就SQL语句需要变为动态的
declare @str varchar(100)
set @str = 'select * from Table where ' + @col + '=' + @KeyWord
exec(@str)
declare @str varchar(100)
set @str = 'select * from Table where ' + @col + '=' + @KeyWord
exec(@str)
追问
执行过程的时候会报错 比如说我的@col输入为“学生”,@KeyWord输入为“张三”,它会弹出说列名“张三”无效,这是为什么
追答
如果你那字段是字符串,那么就要改为:
declare @str varchar(100)
set @str = 'select * from Table where ' + @col + '=''' + @KeyWord + ''''
exec(@str)
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
CREATE PROCEDURE HumanResources.uspGetEmployeesTest2
@LastName nvarchar(50),
@FirstName nvarchar(50)
AS
SET NOCOUNT ON;
SELECT FirstName, LastName, Department
FROM HumanResources.vEmployeeDepartmentHistory
WHERE FirstName = @FirstName AND LastName = @LastName
AND EndDate IS NULL;
GO
@LastName nvarchar(50),
@FirstName nvarchar(50)
AS
SET NOCOUNT ON;
SELECT FirstName, LastName, Department
FROM HumanResources.vEmployeeDepartmentHistory
WHERE FirstName = @FirstName AND LastName = @LastName
AND EndDate IS NULL;
GO
追问
是回答我提的问题的吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询