存储过程参数的问题

来源:百度知道 编辑:UC知道 时间:2024/05/22 04:12:36
先看一个过程
CREATE PROCEDURE [dbo].[CreateTB]
@DDate char(10)
AS
GO
--1
declare @Date char(11)
declare @sql char(2000)
set @Date='zxT' + rtrim(@DDate)
set @sql=''
if exists(select * from dbo.sysobjects where name like rtrim(@date))
begin
set @sql='drop table ' + @date
set @sql=rtrim(@sql)
execute(@sql)
end
set @sql=rtrim('create table ' +rtrim(@Date) +'([Id] [int] NOT NULL ,[M1] [char] (10) COLLATE Chinese_PRC_CI_AS NOT NULL , [M2] [money] NULL ,[M3] [money] NULL ,
[M4] [money] NULL ,
[M5] [money] NULL ,
[M6] [money] NULL ,
[M7] [money] NULL ,
[M8] [money] NULL ,
[M9] [money] NULL ,
[M10] [money] NULL ,
[M11] [money] NULL
) ')
execute(@sql)
set @sql=''
go
上面的过程很简单但在查询分析器里验证的时候,提示下面的信息:
服务器: 消息 137,级别 15,状态 2,行 4
必须声明变量 '@DDate'。
改为Function 后还是不行
CREATE Function

看了一下!

问题出现在这里!

CREATE PROCEDURE [dbo].[CreateTB]
@DDate char(10)
AS
GO
go是完毕的意思

所以后面的sql就不知道有@DDate这么一个参数

所以把go去掉就好了

你试试看

CREATE PROCEDURE [dbo].[CreateTB]
@DDate char(10)
AS

--1
declare @Date char(11)
declare @sql char(2000)
set @Date='zxT' + rtrim(@DDate)
set @sql=''
if exists(select * from dbo.sysobjects where name like rtrim(@date))
begin
set @sql='drop table ' + @date
set @sql=rtrim(@sql)
execute(@sql)
end
set @sql=rtrim('create table ' +rtrim(@Date) +'([Id] [int] NOT NULL ,[M1] [char] (10) COLLATE Chinese_PRC_CI_AS NOT NULL , [M2] [money] NULL ,[M3] [money] NULL ,
[M4] [money] NULL ,
[M5] [money] NULL ,
[M6] [money] NULL ,
[M7] [money] NULL ,
[M8] [money] NULL ,
[M9] [money] NULL ,
[M10] [money] NULL ,
[M11] [money] N