----以下是我的代码----set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
goALTER procedure [dbo].[InsertPhotoRecord]
@UserID nvarchar(5),
@CollectID nvarchar(20),
@Description nvarchar(50),
@FileExtension varchar(20),
@FileName varchar(50) output
AS
BEGIN
---code file---
Declare @PhotoNum as nvarchar(20),@Photo as nvarchar(20),
@strsqlInsert as nvarchar(max),@strsqlSelect as nvarchar(max),
@strsqlUpdate as nvarchar(max),@strsqlOutput as nvarchar(max);
set @Photo='U'+@UserID+'_Photo'
--构建SQL语句--
set @strsqlInsert='insert into '+@Photo+'(所属相册,相片描述,上传时间)
values('+@CollectID+','''+@Description+''',getDate())'set @strsqlSelect='select top 1 '+@PhotoNum+'=相片编号 from ['+@Photo+'] order by 上传时间 desc'
set @strsqlUpdate='update ['+@Photo+'] set 相片文件名='+@PhotoNum+ @FileExtension+' where 相片编号='+@PhotoNum+''
set @strsqlOutput='select '+@FileName+'=相片文件名 from ['+@Photo+'] where 相片编号='+@PhotoNum+''
--生成并执行--
PRINT @strsqlInsert--插入新纪录
PRINT @strsqlSelect--获取最新插入的记录编号
PRINT @strsqlUpdate--在最新记录中插入相片文件名
PRINT @strsqlOutput--输出最新插入的相片文件名exec (@strsqlInsert)
exec (@strsqlSelect)
exec (@strsqlUpdate)
exec (@strsqlOutput)
--By 周海健--
END
主要我想实现的功能是根据传出参数不一样实现在不同的表中插入新记录
如传入参数为(按照声明的顺序):
@UserID = '50015' --用户ID,用于生成用户对应的相片列表[格式:U+ID+_Photo |如U50015_Photo]
@CollectID ='1' --这个图片属于哪个相册
@Description ='这是一张图片' --图片描述
@FileExtension ='.JPG' --这个图片的格式
然后在[U50015_Photo]这个表中插入一条新纪录,然后输出新纪录名.
这些代码能通过检测,但是实际运行的时候...却出现了很严重的问题.
问题如下:
第一句代码能成功运行,即能在对应的表中成功插入一个新记录.
第二句,第三句,第四句显示"1行受影响",但是没有任何效果,即在数据库中[相片文件名]列名是空的,实际没插入.问题出在哪里?应该怎么修改?高人啊...打救一下我吧...
set QUOTED_IDENTIFIER ON
goALTER procedure [dbo].[InsertPhotoRecord]
@UserID nvarchar(5),
@CollectID nvarchar(20),
@Description nvarchar(50),
@FileExtension varchar(20),
@FileName varchar(50) output
AS
BEGIN
---code file---
Declare @PhotoNum as nvarchar(20),@Photo as nvarchar(20),
@strsqlInsert as nvarchar(max),@strsqlSelect as nvarchar(max),
@strsqlUpdate as nvarchar(max),@strsqlOutput as nvarchar(max);
set @Photo='U'+@UserID+'_Photo'
--构建SQL语句--
set @strsqlInsert='insert into '+@Photo+'(所属相册,相片描述,上传时间)
values('+@CollectID+','''+@Description+''',getDate())'set @strsqlSelect='select top 1 '+@PhotoNum+'=相片编号 from ['+@Photo+'] order by 上传时间 desc'
set @strsqlUpdate='update ['+@Photo+'] set 相片文件名='+@PhotoNum+ @FileExtension+' where 相片编号='+@PhotoNum+''
set @strsqlOutput='select '+@FileName+'=相片文件名 from ['+@Photo+'] where 相片编号='+@PhotoNum+''
--生成并执行--
PRINT @strsqlInsert--插入新纪录
PRINT @strsqlSelect--获取最新插入的记录编号
PRINT @strsqlUpdate--在最新记录中插入相片文件名
PRINT @strsqlOutput--输出最新插入的相片文件名exec (@strsqlInsert)
exec (@strsqlSelect)
exec (@strsqlUpdate)
exec (@strsqlOutput)
--By 周海健--
END
主要我想实现的功能是根据传出参数不一样实现在不同的表中插入新记录
如传入参数为(按照声明的顺序):
@UserID = '50015' --用户ID,用于生成用户对应的相片列表[格式:U+ID+_Photo |如U50015_Photo]
@CollectID ='1' --这个图片属于哪个相册
@Description ='这是一张图片' --图片描述
@FileExtension ='.JPG' --这个图片的格式
然后在[U50015_Photo]这个表中插入一条新纪录,然后输出新纪录名.
这些代码能通过检测,但是实际运行的时候...却出现了很严重的问题.
问题如下:
第一句代码能成功运行,即能在对应的表中成功插入一个新记录.
第二句,第三句,第四句显示"1行受影响",但是没有任何效果,即在数据库中[相片文件名]列名是空的,实际没插入.问题出在哪里?应该怎么修改?高人啊...打救一下我吧...
解决方案 »
- 求一个解决办法或SQL语句
- 散分!请教高手,,数据检索的问题。。。。。。。
- 求一个sqlserver自带函数,名称好像是dump开头的,或者不是。。。 实现的功能是将参数varchar重复累加,具体见内
- 新开贴,再问个Sql问题,希望各位能帮解决一下,这个急需,分不够可再加
- 为什么我安装的sql没有智能提示啊?
- SQL中能否达到EXCEL公式的功能?
- 急! ! ! ! !急! ! ! ! !着急啊! ! ! ! !关于外部数据入库问题!(分不够可再加)
- 无法打开数据库~请帮助
- 哪里错了?将数据库创建到指定目录的问题。
- [★★★ 急...] 一个 Sql 多条件查询 语句 报错 ??
- SQL 2008如何整合数据中的有同一值得数据
- sql 年龄分组
set @strsqlOutput='select '+@FileName+'=相片文件名 from ['+@Photo+'] where 相片编号='+@PhotoNum+''分明就是select @FileName,@FileName是传入的,不理解先插入,又更新,在得到这个固定变量有什么意义。
你的存储过程无法运行,是因为在同一个对话中无法直接取出ID值,要用 @@IDENTITY 取.而且最好还要加事务,否则可能会遇到并发错误.
你这个需求,还不如用触发器更好些.
create table U50015_Photo(相片编号 int identity(1,1),所属相册 int,相片描述 nvarchar(100),上传时间 datetime,相片文件名 varchar(50))
go
create trigger updatefilename
on U50015_Photo
after insert
as
update u50015_Photo set 相片文件名=ltrim(相片编号)+相片文件名
--By qianjin036a
go
create procedure [dbo].[InsertPhotoRecord]
@UserID nvarchar(5),
@CollectID nvarchar(20),
@Description nvarchar(50),
@FileExtension varchar(20),
@FileName varchar(50) output
AS
BEGIN
---code file---
Declare @PhotoNum as nvarchar(20),@Photo as nvarchar(20),
@strsqlInsert as nvarchar(max),@strsqlSelect as nvarchar(max),
@strsqlUpdate as nvarchar(max),@strsqlOutput as nvarchar(max);
set @Photo='U'+@UserID+'_Photo'
--构建SQL语句--
set @strsqlInsert='insert into '+@Photo+'(所属相册,相片描述,上传时间,相片文件名)
select '+@CollectID+','''+@Description+''',getDate(),'''+@FileName+@FileExtension+''''
--生成并执行--
exec (@strsqlInsert)
--By 周海健--
END
go
exec InsertPhotoRecord '50015','1','sssssszsdf','.jpg','P616645646'
select * from U50015_Photo
/*
相片编号 所属相册 相片描述 上传时间 相片文件名
----------- ----------- ------------------------------------ -------------------------- -----------------------------------------------
1 1 sssssszsdf 2011-12-18 21:25:54.010 1P616645646.jpg(1 行受影响)*/
go
drop table U50015_Photo
drop procedure InsertPhotoRecord甚至更简单地可以直接什么也不改,要取的时候直接从表中查询文件名,且在前面加上一个编号:select ltrim(相片编号)+相片文件名 from u50015
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
goALTER PROCEDURE [dbo].[CreateNewRecord]
@UserID nvarchar(5)
AS
BEGIN
--code file--
--构建SQL语句--
Declare @Collect as nvarchar(20), @Photo as nvarchar(20);
Declare @StrSqlCollect as nvarchar(max), @StrSqlPhoto as nvarchar(max);
set @Collect='U'+@UserID+'_Collect'
set @Photo='U'+@UserID+'_Photo'
--相册--
set @StrSqlCollect = 'create table '+@Collect+'
(
[相册编号] int IDENTITY (1,1) PRIMARY KEY ,
[相册名称] nvarchar(10) not null ,
[上级相册] smallint null ,
[相册描述] nvarchar(50) null,
[创建时间] datetime null,
)'
--相片--
SET @StrSqlPhoto = 'create table '+@Photo+'
(
[相片编号] int IDENTITY (201200001,1) PRIMARY KEY ,
[所属相册] smallint not null ,
[相片描述] nvarchar(50) null ,
[相片文件名] nvarchar(50) null,
[点击率] int null,
[上传时间] datetime null,
)'
--生成并执行--
PRINT @StrSqlCollect
PRINT @StrSqlPhoto
exec (@StrSqlCollect)
exec (@StrSqlPhoto)
--By 周海健--
END
第一次来到CSDN.感谢各位的热心帮助.
我看了给我回复的大侠的内容,感觉真的学到东西了.
后来我自己再继续尝试继续修改,最后终于还是成功了.
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER procedure [dbo].[InsertPhotoRecord]
@UserID nvarchar(5),
@CollectID nvarchar(20),
@Description nvarchar(50),
@FileExtension varchar(20),
@FileName varchar(50) output
AS
BEGIN
---code file---
Declare @PhotoNum as nvarchar(20),@Photo as nvarchar(20),@strFileName as nvarchar(20);
Declare @strsqlInsert as nvarchar(max),@GetFileName as nvarchar(max);
set @Photo='U'+@UserID+'_Photo'
--构建SQL语句--
set @strsqlInsert='insert into '+@Photo+'(所属相册,相片描述,上传时间)
values('+@CollectID+','''+@Description+''',getDate())'set @GetFileName='Declare @PhotoNum as nvarchar(20);
select top 1 @PhotoNum=相片编号 from ['+@Photo+'] order by 上传时间 desc;
update ['+@Photo+'] set 相片文件名=''''+@PhotoNum+'''+''+@FileExtension+''' where 相片编号=@PhotoNum;
select @FullFileName=相片文件名 from ['+@Photo+'] where 相片编号=@PhotoNum;
'
PRINT @strsqlInsert--debug输出
exec (@strsqlInsert)--执行PRINT (@GetFileName)--debug输出
--执行并获取返回值--
EXEC sp_executesql @GetFileName,N'@FullFileName nvarchar(20) output',@strFileName output
--输出返回值
set @FileName=@strFileName--By 周海健--
END
晕..居然贴错代码了.这个才是的.
上面那个是创建表的.