UserID
NoteID
and will contain something like
UserID = '1, 2, 3, 4'
NoteID = '4, 9, 18, 21, 23, 27'
However its not known the length of each and so we could have the reverse of the above.
I am to insert x amount of notes to one userId and vice versa, but I'm trying to figure out how to do both so the insert would resemble the following.
UserID NoteID
1 4
1 9
1 18
1 21
1 23
1 27
2 4
2 9
etc, etc
This is sp that does it and it works fine for just one or the other, I just don't have much experience in this kind of thing. The spInsertAssignedNoteDetail at the end simply makes the insert when I have both numbers.
Heres my attempt, but i'm just stuck as to where to go from here
CREATE PROCEDURE spInsertAssignedNotesByList
@.FK_UserIDList NVARCHAR(4000) = NULL,
@.FK_NoteIDList NVARCHAR(4000) = NULL
AS
SET NOCOUNT ON
DECLARE @.Length INT
DECLARE @.Note_Length INT
DECLARE @.FirstUserIDWord NVARCHAR(4000)
DECLARE @.FK_UserID INT
DECLARE @.FK_NoteID INT
SELECT @.Length = DATALENGTH(@.FK_UserIDList )
SELECT @.Note_Length = DATALENGTH(@.FK_NoteIDList )
WHILE @.Length > 0 or @.Note_Length > 0
BEGIN
EXECUTE @.Length = PopFirstWord @.FK_UserIDList OUTPUT, @.FirstUserIDWord OUTPUT
IF @.Length > 0
BEGIN
SELECT @.FK_UserID = CONVERT(INT, @.FirstUserIDWord)
EXECUTE spInsertAssignedNoteDetail @.FK_UserID, @.FK_NoteID
END
END
----------------
GOOk I got this far, which does what I want with the first loop but when it comes to do the second outer do while for some reason the counter is zero instead of going backing to what it was when it started. Anyone know why it does this. Thanks
CREATE PROCEDURE spInsertAssignedNotesByList
@.FK_UserIDList NVARCHAR(4000) = NULL,
@.FK_NoteIDList NVARCHAR(4000) = NULL
AS
SET NOCOUNT ON
DECLARE @.Length INT
DECLARE @.Note_Length INT
DECLARE @.FirstUserIDWord NVARCHAR(4000)
DECLARE @.FirstNoteIDWord NVARCHAR(4000)
DECLARE @.FK_UserID INT
DECLARE @.FK_NoteID INT
SELECT @.Length = DATALENGTH(@.FK_UserIDList )
SELECT @.Note_Length = DATALENGTH(@.FK_NoteIDList )
WHILE @.Length > 0
BEGIN
IF @.Length > 0
EXECUTE @.Length = PopFirstWord @.FK_UserIDList OUTPUT, @.FirstUserIDWord OUTPUT
SELECT @.FK_UserID = CONVERT(INT, @.FirstUserIDWord)
WHILE @.Note_Length > 0
BEGIN
EXECUTE @.Note_Length = PopFirstWord @.FK_NoteIDList OUTPUT, @.FirstNoteIDWord OUTPUT
SELECT @.FK_NoteID = CONVERT(INT, @.FirstNoteIDWord)
IF @.Note_Length > 0
EXECUTE spInsertAssignedNoteDetail @.FK_UserID, @.FK_NoteID
END
END
----------------
GO
No comments:
Post a Comment