alter procedure cursor0 @monthname nchar(9),@date char(2),@spbh int
as 
begin
declare @strSQL nvarchar(1000)
declare @cursordate char(2)
set @strSQL='declare deletemonth cursor LOCAL scroll for Select Name FROM SysColumns Where id=Object_Id('+''''+@monthname+''''+') and name>='+''''+@date+''''+' order by name
for update open deletemonth
fetch next from deletemonth
into '+ltrim(@cursordate)+'
while(@@fetch_status=0)
begin
set @strSQL=update ['+@monthname+ ']set ['+@cursordate+']=0 where ['+@monthname+'].spbh='+@spbh+
'fetch next from deletemonth
into '+ltrim(@cursordate)+'
end
close deletemonth
deallocate deletemonth'
exec sp_executesql @strSQL
end
USE [syb]
GO
DECLARE @return_value int
EXEC @return_value = [dbo].[cursor0]
@monthname = N'dataout04',
@date = N'10',
@spbh = 45696
SELECT 'Return Value' = @return_value
GOset ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
goALTER procedure [dbo].[createbase] @is_ct nchar(2) as
begin
---定义相关变量
---是否重新创建表,如果重新创建表会报现有的表与数据全部清空
if @is_ct='是'
begin--1创建表
--a.基础数据表sp 
if not exists(SELECT * FROM syb..SysObjects Where XType='U' and name='sp')
begin
create table sp(spid int not null primary key,spbh int not null ,spmch nchar(255),status nchar(4) default '可售'
    ,leixing int,[datetime0] char(20) default convert(char(20),getdate(),20),[datetime1] char(20) default convert(char(20),getdate(),20))--0为启售日期,1 停售日期,当两个相同是从开始就启用了
end
else 
begin
--弹出消息告知已存在是否继续执行
drop table sp
create table sp(spid int not null primary key,spbh int not null ,spmch nchar(255),status nchar(4) default '可售'
    ,leixing int,[datetime0] char(20) default convert(char(20),getdate(),20),[datetime1] char(20) default convert(char(20),getdate(),20))--0为启售日期,1 停售日期,当两个相同是从开始就启用了
end
exec sp_configure 'show advanced options',1  --配置操作不然会导入数据不成功
reconfigure 
exec sp_configure 'Ad Hoc Distributed Queries',1 
reconfigure
truncate table sp
insert into sp(spid,spbh,spmch,leixing) SELECT spid,spbh,spmch,leixing FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=K:\sybtg\基础资料\商品编号.xls;User ID=Admin;Password=;Extended properties="Excel 8.0;HDR=Yes;";Persist Security Info=False')...[sp$]
--b.基础数据表bm
if not exists(select * from syb..sysobjects where xtype='U' and name='bm')
begin  
create table bm(bmid int not null primary key,bmbh int not null ,bmmch nchar(255),status nchar(4) default '可用'
    ,[datetime0] char(20) default convert(char(20),getdate(),20),[datetime1] char(20) default convert(char(20),getdate(),20))--0为启用日期,1 停用日期,当两个相同是从开始就启用了,或者新增的
end
else
begin
--弹出消息告知已存在是否继续执行
drop table bm
create table bm(bmid int not null primary key,bmbh int not null ,bmmch nchar(255),status nchar(4) default '可用'
    ,[datetime0] char(20) default convert(char(20),getdate(),20),[datetime1] char(20) default convert(char(20),getdate(),20))--0为启用日期,1 停用日期,当两个相同是从开始就启用了,或者新增的
end
--同时在这导入部门基础数据
------------------------------------------------------------------------------------
--在这同上面一样用函数来调用就行了
------------------------------------------------------------------------------------
truncate table bm
insert into bm(bmid,bmbh,bmmch) SELECT bmid,bmbh,bmmch FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=K:\sybtg\基础资料\分店资料.xls;User ID=Admin;Password=;Extended properties="Excel 8.0;HDR=Yes;";Persist Security Info=False')...[bm$]
---对月份表的创建
declare @monthname nchar(9) ,@strSQL nvarchar(1000)
declare @i int,@ivar char(2)
set @monthname='dataout01'
set @i=0
while @i<12
begin
set @monthname=left(@monthname,7)
    set @ivar=datename ( month, dateadd(month, @i, getdate()) )
set @monthname=rtrim(ltrim(@monthname))+@ivar
if not exists(select * from syb..sysobjects where xtype='u' and name=@monthname)
begin
select @strSQL=dbo.getstrdataout(@monthname)--调用建表函数字串
EXEC sp_ExecuteSql @strSQL
set @strSQL='insert into ['+@monthname+'](spbh,spmch,bmbh,bmmch,leixing)select a.spbh,a.spmch,b.bmbh,b.bmmch,a.leixing from sp a,bm b'
EXEC sp_ExecuteSql @strSQL
end
else
begin
set @strSQL='drop table ['+@monthname+']'
EXEC sp_ExecuteSql @strSQL
select @strSQL=dbo.getstrdataout(@monthname)--调用建表函数字串
EXEC sp_ExecuteSql @strSQL
set @strSQL='insert into ['+@monthname+'](spbh,spmch,bmbh,bmmch,leixing)select a.spbh,a.spmch,b.bmbh,b.bmmch,a.leixing from sp a,bm b'
EXEC sp_ExecuteSql @strSQL
end
   set @i = @i + 1
end
return  0 --0值表示成功
end
else
return  1--是返回的整型值,(非整返回不了)除非特别指明,所有系统存储过程返回,0值表示成功,返回非零值则表示失败。end---入出参数说明,当输入参数为'是'才能进行创建,返回0表示操作成功,返回1表示操作失败
---今天要做的:3给sp,bm,增加一个状态重新来操作相关数据,给商品增加一个类别归属属性
--配置操作做成一个函数,由于在函数内的 'CONFIGURE' 中对带副作用的或依赖于时间的运算符的使用无效。
代码里导入的文件放在http://ufofind.gicp.net

解决方案 »

  1.   

    够复杂的,用print检查过拼接后的SQL语句没有?
      

  2.   

    改一下那个弄错了导入的数据文件放在ftp://ufofind.gicp.net
      

  3.   


    --这样不报错
    declare @cursordate int
    set @cursordate=11select 'fetch next from deletemonth into '+ltrim(@cursordate)--这样才报错
    declare @cursordate1 int
    set @cursordate1=11select 'fetch next from deletemonth into '+@cursordate1
      

  4.   

    发现问题:alter procedure cursor0 @monthname nchar(9),@date char(2),@spbh int
    as 
    begin
    declare @strSQL nvarchar(1000)
    declare @cursordate char(2)
    set @strSQL='declare deletemonth cursor LOCAL scroll for Select Name FROM SysColumns Where id=Object_Id('+''''+@monthname+''''+') and name>='+''''+@date+''''+' order by name
    for update open deletemonth
    fetch next from deletemonth
    into '+ltrim(@cursordate)+'
    while(@@fetch_status=0)
    begin
        set @strSQL=update ['+@monthname+ ']set ['+@cursordate+']=0 where ['+@monthname+'].spbh='+@spbh+  --这个位置@spbh是int的直接加字符串不行的,需要用ltrim隐式转换一下
        'fetch next from deletemonth
        into '+ltrim(@cursordate)+'
    end
    close deletemonth
    deallocate deletemonth'
    exec sp_executesql @strSQL
    end
      

  5.   

    谢谢高手们的指教,现在是不报错了,但是没有达到想要update数据的效果,叶子这时候还没有睡,现在早点睡吧有时间再帮我看一下我在游标怎样实现数据的更新操作
      

  6.   

    组合SQL语句,对于字符型数据没掌握好吧?你把下面的看懂了就明白了.--动态sql语句基本语法
     
    1 :普通SQL语句可以用Exec执行 eg: Select * from tableName 
    Exec('select * from tableName') 
    Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N 2:字段名,表名,数据库名之类作为变量时,必须用动态SQL eg: 
    declare @fname varchar(20) 
    set @fname = 'FiledName' 
    Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。 
    Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格 当然将字符串改成变量的形式也可 
    declare @fname varchar(20) 
    set @fname = 'FiledName' --设置字段名 declare @s varchar(1000) 
    set @s = 'select ' + @fname + ' from tableName' 
    Exec(@s) -- 成功 
    exec sp_executesql @s -- 此句会报错 declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000) 
    set @s = 'select ' + @fname + ' from tableName' 
    Exec(@s) -- 成功 
    exec sp_executesql @s -- 此句正确 3. 输出参数 
    declare @num int, 
    @sqls nvarchar(4000) 
    set @sqls='select count(*) from tableName' 
    exec(@sqls) 
    --如何将exec执行结果放入变量中? declare @num int, 
    @sqls nvarchar(4000) 
    set @sqls='select @a=count(*) from tableName ' 
    exec sp_executesql @sqls,N'@a int output',@num output 
    select @num 
      

  7.   


    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    goALTER procedure [dbo].[cursor0] @monthname nchar(9),@date char(2),@spbh int
    as 
    begin
    declare @strSQL nvarchar(1000)
    declare @cursordate char(2)
    set @monthname='dataout04'
    set @date='10'
    set @strSQL='declare deletemonth cursor LOCAL scroll for Select Name FROM SysColumns Where id=Object_Id('+''''+@monthname+''''+') and name>='+''''+@date+''''+'and name<='+'''31'''+' order by name
    for update open deletemonth
    fetch next from deletemonth
    into '+ltrim(@cursordate)+'
    while(@@fetch_status=0)
    begin
        set @strSQL=update ['+@monthname+ ']set ['+@cursordate+']=0 where ['+@monthname+'].spbh='+ltrim(@spbh)+  --这个位置@spbh是int的直接加字符串不行的,需要用ltrim隐式转换一下
        'fetch next from deletemonth
        into '+ltrim(@cursordate)+'
    end
    close deletemonth
    deallocate deletemonth'
    exec sp_executesql @strSQL
    end
    改成了这样还是达不到效果
      

  8.   

    --1. 使用 EXEC 实现的动态参数存储过程
    CREATE PROC p_test
    @para1 varchar(10)=null,
    @para2 varchar(10)=null,
    @para3 varchar(10)=null,
    @para4 varchar(10)=null
    AS
    SET NOCOUNT ON
    DECLARE @sql varchar(8000)
    SET @sql='SELECT * FROM tbname WHERE 1=1'
    IF @para1 IS NOT NULL
    SET @sql=@sql+' AND col1='''+@para1+''''
    IF @para2 IS NOT NULL
    SET @sql=@sql+' AND col2='''+@para2+''''
    IF @para3 IS NOT NULL
    SET @sql=@sql+' AND col3='''+@para3+''''
    IF @para4 IS NOT NULL
    SET @sql=@sql+' AND col4='''+@para4+''''
    EXEC(@sql)
    GO
    /*======================================================*/--2. 使用 sp_executesql 实现的动态参数存储过程
    CREATE PROC p_test
    @para1 varchar(10)=null,
    @para2 datetime=null,
    @para3 varchar(10)=null,
    @para4 int=null
    AS
    SET NOCOUNT ON
    DECLARE @sql nvarchar(4000)
    SET @sql='SELECT * FROM tbname WHERE 1=1'
    +CASE WHEN @para1 IS NULL THEN '' ELSE ' AND col1=@para1' END
    +CASE WHEN @para2 IS NULL THEN '' ELSE ' AND col2=@para2' END
    +CASE WHEN @para3 IS NULL THEN '' ELSE ' AND col3=@para3' END
    +CASE WHEN @para4 IS NULL THEN '' ELSE ' AND col4=@para4' END
    EXEC sp_executesql @sql,N'
    @para1 varchar(10)=null,
    @para2 datetime=null,
    @para3 varchar(10)=null,
    @para4 int=null
    ',@para1,@para2,@para3,@para4
    GO
    /*======================================================*/--3. 不使用动态 Transact-SQL 语句实现的动态参数存储过程
    CREATE PROC p_test
    @para1 varchar(10)=null,
    @para2 datetime=null,
    @para3 varchar(10)=null,
    @para4 int=null
    AS
    SET NOCOUNT ON
    SELECT * FROM tbname 
    WHERE (@para1 IS NULL OR col1=@para1)
    AND (@para2 IS NULL OR col2=@para2)
    AND (@para3 IS NULL OR col3=@para3)
    AND (@para4 IS NULL OR col4=@para4)
      

  9.   

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go
    ALTER procedure [dbo].[cursor2] @monthname nchar(9),@date char(2),@spbh int
    as 
    begin
    declare @cursordate char(2)
    declare @strSQL nvarchar(1000)
    set @strSQL='declare deletemonth cursor LOCAL scroll for Select Name FROM SysColumns Where id=Object_Id('+''''+@monthname+''''+') 
    for update open deletemonth
    fetch next from deletemonth
    into '+@cursordate
    exec sp_executesql @strSQL
    while(@@fetch_status=0)
    begin
    set @strSQL='update ['+@monthname+ ']set ['+@cursordate+']=0 where ['+@monthname+'].spbh='+@spbh
    exec sp_executesql @strSQL
    fetch next from deletemonth
    into @cursordate
    end
    close deletemonth
    deallocate deletemonth
    end
    消息 16916,级别 16,状态 1,过程 cursor2,第 20 行
    名为 'deletemonth' 的游标不存在。
    消息 16916,级别 16,状态 1,过程 cursor2,第 21 行
    名为 'deletemonth' 的游标不存在。
      

  10.   

    我的妈也终于找到了为什么没
    消息 16916,级别 16,状态 1,过程 cursor2,第 11 行
    名为 'abc' 的游标不存在。
    原来我在使用 动态语句游标的时候使用了 local导致的