sql 2005关于存储过程的问题
create proc attach_db
as
begin
declare @dbname varchar(50),@i int,@dbPath varchar(50),@ldfPath varchar(50),@mdfPath varchar(50),@sql varchar(1000),@rq varchar(100)
declare mycs cursor FORWARD_ONLY for select distinct xzqh_bm from xzqh
open mycs
set @i = (select count(*) from (select distinct xzqh_bm from xzqh) as xzqh)
fetch next from mycs into @dbname
set @rq=CONVERT(VARCHAR(6),GETDATE(),112)-1
set @mdfPath='D:\zrzjk\CWJJJG' + @rq + '\' + @dbname + '.mdf'
set @ldfPath='D:\zrzjk\CWJJJG' + @rq + '\' + @dbname + '_log.ldf'
set @sql='sp_attach_db ''' + @dbname +''', ''' + @mdfPath + ''' , ''' + @ldfPath + ''' '
exec (@sql)
while @i > 1
begin
fetch next from mycs into @dbname
set @rq=CONVERT(VARCHAR(6),GETDATE(),112)-1
set @mdfPath='D:\zrzjk\CWJJJG' + @rq + '\' + @dbname + '.mdf'
set @ldfPath='D:\zrzjk\CWJJJG' + @rq + '\' + @dbname + '_log.ldf'
set @sql='sp_attach_db ''' + @dbname +''', ''' + @mdfPath + ''' , ''' + @ldfPath + ''' '
exec (@sql)
set @i=@i-1
end
close mycs
DEALLOCATE mycs
end
这是创建的存储过程,执行的时候老是报错:消息 102,级别 15,状态 1,第 1 行
'371602' 附近有语法错误。'371602' 这个是xzqh 。
初学者,找不到原因,求解啊!!
------解决方案--------------------再试试,谁叫你用数字来做库名的首字符
create proc [dbo].[createDbTable]
as
begin
declare @rc nvarchar(100),@dbname varchar(100),@dblogname varchar(100),@mdfPath varchar(100),@ldfPath varchar(100),@createDbSql varchar(1000)
declare @i int,@j int,@dbPath varchar(100),@kjqj varchar(50),@gsdm varchar(50),@zth varchar(50),@kjnd varchar(50),@rq varchar(50),@pznrSql varchar(1000)
declare @pzmlSql varchar(1000),@fzxlbSql varchar(1000),@fzxzlSql varchar(1000),@kmxxSql varchar(1000),@yebSql varchar(1000),@ztcsSql varchar(1000),@pubkszlSql varchar(1000),@pubzyxxSql varchar(1000)
declare mycs cursor FORWARD_ONLY for select distinct xzqh_bm from xzqh
set @i =( select count(*) from xzqh )
set @j=@i
set @rq=CONVERT(VARCHAR(6),GETDATE(),112)-1