如何根据SQL Server 2005中的参数编写创建视图的过程

时间:2009-08-19 15:30:18

标签: sql sql-server sql-server-2005

我想编写一个根据SQL Server 2005中的参数创建视图的过程。

总的来说,这应该是这样的:

CREATE PROCEDURE testprocedure
@clientid float
as
create view testview as 
select * from deliveries where clientid=@clientid

(但这不起作用,它会给出错误:关键字'view'附近的语法不正确。)

有可能吗?

2 个答案:

答案 0 :(得分:3)

尝试:

create procedure testprocedure
@clientid float
as
declare @query  varchar(8000)
set @query='create view testview as select * from deliveries where clientid='+CONVERT(varchar(20),@clientid)
IF  EXISTS (SELECT object_id FROM sys.views WHERE object_id = OBJECT_ID('testview'))
BEGIN
    DROP VIEW testview
END
exec (@Query)

go

答案 1 :(得分:1)

不确定我会推荐这个作为任何问题的解决方案,但你可以尝试这样的事情:(动态sql)

CREATE PROCEDURE testprocedure

@clientid float

作为

DECLARE @sql varchar(8000)设置@sql =''

SET @sql = @sql +'创建视图testview为'

SET @sql = @sql +'select * from deliveryies clientid = @ clientid'

SET @sql = @sql + cast(@clientid as varchar(50))

EXEC(@sql)