日期:2014-05-18 浏览次数:20749 次
create proc [dbo].[usp_Insert] @F1 VARCHAR(300)=' ', @F2 VARCHAR(12)=' ' as create table #t1 ( col varchar(12) ) set @F1=replace(@F1,',',''' union all select ''') set @F1='insert into #t1 select '''+@F1+'''' exec (@F1) insert into tablename ( F1, F2, DATES ) select col, @F2, getdate() from #t1 drop table #t1
------解决方案--------------------
--> 测试数据:[tb] if object_id('[tb]') is not null drop table [tb] go create table [tb]([F1] varchar(20),[F2] varchar(20),[DATES] datetime) DECLARE @f1 VARCHAR(300)='B07100400731,B07100400732,B07100400733,B07100400734' DECLARE @f2 VARCHAR(30)='A00000000001' DECLARE @s VARCHAR(300)=@f1 WHILE CHARINDEX(',',@s)>0 BEGIN INSERT INTO dbo.tb VALUES (LEFT(@s,CHARINDEX(',',@s)-1), @f2,GETDATE()) SET @s=REPLACE(@s,LEFT(@s,CHARINDEX(',',@s)),'') END select * from [tb]