January 15, 2014 at 11:35 pm
I have a table
Create table testmuljobs
(
AutoID int Identity(1,1),
JobID int not null,
AcctID int not null,
CreatedBy Varchar(100) null,
CreatedDate Datetime null,
ModifiedBy Varchar(100) null,
ModifiedDate Datetime null,
RecStatus bit null
)
and for CreatedDate and ModifiedDate i have Default Constraints
I want to insert records into that table with the help of sp as follows:
i will pass Jobid and Acctid .The Sample example stored procedure is
Create PROCEDURE [dbo].[TestMulJobsSave]
(
@JobID INT = Null,
@AcctID INT = Null,
@createdby VARCHAR(100) = Null,
@ModifiedBy VARCHAR(100) = Null
)
AS
Begin
Insert into [TestMulJobs] (JobID,AcctID,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate) Values
( @JobID,@AcctID,@CreatedBy,Getdate(),@ModifiedBy, Getdate() )
End
now when i execute the sp like for example TestMulJobsSave 5653,7 it is inserting the records and working fine,but my requirement is instead i want to insert as 5654-7, 5723-8, 5824-3,5654-7.All the above must be inserted as 4 records the first one is jobid and the corresponding to it is the AcctID i.e i want to insert multiple acctid and the corresponding jobiD at a time.
January 16, 2014 at 3:50 am
Try this if it helps you.
CREATE FUNCTION [dbo].[fnSplit]
(
@sInputList VARCHAR(8000) -- List of delimited items
, @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
) RETURNS @List TABLE (item VARCHAR(8000))
BEGIN
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
BEGIN
SELECT
@sItem=SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1),
@sInputList=SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+DATALENGTH(@sDelimiter),DATALENGTH(@sInputList))
IF LEN(@sItem) > 0
INSERT INTO @List SELECT @sItem
ELSE IF LEN(@sItem) = 0
INSERT INTO @List SELECT NULL
END
IF LEN(@sInputList) > 0
INSERT INTO @List SELECT @sInputList -- Put the last item in
RETURN
END
GO
CREATE PROCEDURE [dbo].[TestMulJobsSave]
(
@JobIDANdAcctID VARCHAR(MAX),
@createdby VARCHAR(100) = Null,
@ModifiedBy VARCHAR(100) = Null
)
AS
Begin
Insert into [TestMulJobs] (JobID,AcctID,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate)
SELECT SUBSTRING(Item,1,CHARINDEX('-',Item)-1),SUBSTRING(Item,CHARINDEX('-',Item)+1,LEN(ITEM)),@CreatedBy,@ModifiedBy FROM TEST.dbo.fnSplit(@JobIDANdAcctID,',')
End
GO
EXEC TEST..[TestMulJobsSave] '5654-7,5723-8,5824-3,5654-7',1,1
Regards,
Vijay
January 16, 2014 at 7:50 am
It does seem that what you need it a string splitter. The one posted above will work but the performance of that will be awful. Please take a look at the link in my signature about splitting strings. You will find a way faster string splitter in there. It even includes performance comparisons among all the various types of splitters (including the one posted above).
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
January 16, 2014 at 10:35 pm
@ vijay.singh 14112 :
Thanks a lot for the reply and the help will check it out.
January 17, 2014 at 7:24 am
iiit.raju (1/16/2014)
@ vijay.singh 14112 :Thanks a lot for the reply and the help will check it out.
I will stress again to look at the splitter I reference in my signature. The one posted above will work but it is orders of magnitude slower.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply