SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to get the desired output using Stored Procedure


How to get the desired output using Stored Procedure

Author
Message
iiit.raju
iiit.raju
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 161
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.
vijay.singh 14112
vijay.singh 14112
Mr or Mrs. 500
Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)

Group: General Forum Members
Points: 569 Visits: 185
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
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26113 Visits: 17538
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
iiit.raju
iiit.raju
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 161
@ vijay.singh 14112 :

Thanks a lot for the reply and the help will check it out.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26113 Visits: 17538
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search