alter proc ProductSearch
(
@type nvarchar(50),
@KeyWord nvarchar(255)
)
as EXEC('select *,bookcost*bookRebate as Nowcost from book where '+ @type+' like ''%'+ @KeyWord+'%''')
go
为什么存储过程要用EXEC
什么情况下才用EXEC
说明理由 重分奖赏 顶者1分
...全文
29154打赏收藏
进的人多少都有分
alter proc ProductSearch ( @type nvarchar(50), @KeyWord nvarchar(255) ) as EXEC('select *,bookcost*bookRebate as Nowcost from book where '+ @type+' like ''%'+ @KeyWord+'%''') go 为什么存储过程要用EXEC 什么情况下才用EXEC 说明理由 重分奖赏 顶者1分
[Quote=引用楼主 rewoshengqi 的回复:]
alter proc ProductSearch
(
@type nvarchar(50),
@KeyWord nvarchar(255)
)
as
EXEC('select *,bookcost*bookRebate as Nowcost from book where '+ @type+' like ''%'+ @KeyWord+'%''')
go
为什么存储过……
[/Quote]
同楼主有一样的疑惑,只知道有模糊查询的时候就要写成 EXEC(@sql)的形式,不然存储过程查不到,
添加删除修改那些都可以不用,当然用也可以,
为什么我也不知道.
如果执行字符串有更改数据库上下文的 USE 语句,则对数据库上下文的更改仅持续到 sp_executesql 或 EXECUTE 语句完成。
通过执行下列两个批处理来举例说明:
/* Show not having access to variables from the calling batch. */
DECLARE @CharVariable CHAR(3)
SET @CharVariable = 'abc'
/* sp_executesql fails because @CharVariable has gone out of scope. */
sp_executesql N'PRINT @CharVariable'
GO
/* Show database context resetting after sp_executesql completes. */
USE pubs
GO
sp_executesql N'USE Northwind'
GO
/* This statement fails because the database context
has now returned to pubs. */
SELECT * FROM Shippers
GO
DECLARE @IntVariable INT
DECLARE @SQLString NVARCHAR(500)
/* Build and execute a string with one parameter value. */
SET @IntVariable = 35
SET @SQLString = N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = ' +
CAST(@IntVariable AS NVARCHAR(10))
EXEC(@SQLString)
/* Build and execute a string with a second parameter value. */
SET @IntVariable = 201
SET @SQLString = N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = ' +
CAST(@IntVariable AS NVARCHAR(10))
EXEC(@SQLString)
DECLARE @IntVariable INT
DECLARE @SQLString NVARCHAR(500)
DECLARE @ParmDefinition NVARCHAR(500)
/* Build the SQL string once. */
SET @SQLString =
N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = @level'
/* Specify the parameter format once. */
SET @ParmDefinition = N'@level tinyint'
/* Execute the string with the first parameter value. */
SET @IntVariable = 35
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@level = @IntVariable
/* Execute the same string with the second parameter value. */
SET @IntVariable = 32
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@level = @IntVariable
FETCH NEXT FROM AllDatabases INTO @DBNameVar
WHILE (@@FETCH_STATUS = 0)
BEGIN
PRINT N'CHECKING DATABASE ' + @DBNameVar
SET @Statement = N'USE ' + @DBNameVar + CHAR(13)
+ N'DBCC CHECKDB (' + @DBNameVar + N')'
EXEC sp_executesql @Statement
PRINT CHAR(13) + CHAR(13)
FETCH NEXT FROM AllDatabases INTO @DBNameVar
END
CLOSE AllDatabases
DEALLOCATE AllDatabases
GO
SET NOCOUNT OFF
GO
存储过程的like和execSql 2007-05-15 13:13:26 阅读343 评论0 字号:大中小 订阅
create proc ProductSearch
(
@type nvarchar(50),
@KeyWord nvarchar(255)
)
as
select *,bookcost*bookRebate as Nowcost from book where @type like '%'+@KeyWord+'%'
go
要是单独放出来查询就可以了
select *,bookcost*bookRebate as Nowcost from book where bookname like '%c%'。
但是我用如下存储过程就可以了:
alter proc ProductSearch
(
@type nvarchar(50),
@KeyWord nvarchar(255)
)
as
EXEC('select *,bookcost*bookRebate as Nowcost from book where '+ @type+' like ''%'+ @KeyWord+'%''')
go
我要的是解释为什么要加exec,从csdn论坛得到别人的回答是:
对于字段不能直接使用参数,这个是 sql server解析原理 决定的。
我好象明白了一点点,因为@type传进来的是字段,所以不能直接作为参数。
同时我又获得了另外一个人给我的解释:
create proc ProductSearch
@type nvarchar(50),
@KeyWord nvarchar(255)
as
declare @sql varchar(8000)
set
@sql='select *,bookcost*bookRebate as Nowcost from book where '+ @type+' like ''%'+ @KeyWord+'%'''
exec (@sql)
GO
于是我对我以前做过的普通的不包含把字段作为参数的存储过程也写为上面那种形式,发现
--存储过程查询各类书
方法一:
alter proc BookByCategory
(
@categoryID int
)
as
select BookID,BookName,BookAuthor,BookCost,BookImage,BookTime,BookPublisher,BookIsbn,bookcost*bookrebate as Nowcost,bookrebate
from book
where categoryID=@categoryID
go
方法二:
alter proc BookByCategory
(
@categoryID char(10)
)
as
declare @sql varchar(8000)
set
@sql='select BookID,BookName,BookAuthor,BookCost,BookImage,BookTime,BookPublisher,BookIsbn,bookcost*bookrebate as Nowcost,bookrebate
from book
where categoryID='+@categoryID
exec(@sql)
go
alter proc ProductSearch
(
@type nvarchar(50),
@KeyWord nvarchar(255)
)
as
EXEC('select *,bookcost*bookRebate as Nowcost from book where '+ @type+' like ''%'+ @KeyWord+'%''')
go
alter proc ProductSearch
(
@type nvarchar(50),
@KeyWord nvarchar(255)
)
as
select *,bookcost*bookRebate as Nowcost from book where type like '%'+ @KeyWord+'%'
go
[Quote=引用楼主 rewoshengqi 的回复:]
alter proc ProductSearch
(
@type nvarchar(50),
@KeyWord nvarchar(255)
)
as
EXEC('select *,bookcost*bookRebate as Nowcost from book where '+ @type+' like ''%'+ @KeyWord+'%''')
go
为什么存……
[/Quote]
动态执行语句的时候需要使用Exec