|
-
Dec 8th, 2008, 09:58 PM
#1
Thread Starter
Addicted Member
[RESOLVED] INSERT with Multiple Queries
Hi Guys,
I don't know how to write this in proper SQL Commands:
Code:
INSERT INTO tblTable1
(
Field1,
Field2,
Field3
)
VALUES
(
@Field1,
(SELECT FieldID FROM tblTable2 WHERE FieldName = @FieldName),
(SELECT FieldID FROM tblTable3 WHERE FieldName = @FieldName)
)
That code doesn't work with my Stored Procedure.
Thanks in advance
-
Dec 9th, 2008, 01:04 AM
#2
Re: INSERT with Multiple Queries
Why not something like this?
Code:
DECLARE @value1 nvarchar(200)
DECLARE @value2 nvarchar(200)
SET @value1 = SELECT FieldID FROM tblTable2 WHERE FieldName = @FieldName
SET @value2 = SELECT FieldID FROM tblTable3 WHERE FieldName = @FieldName
INSERT INTO tblTable1
(
Field1,
Field2,
Field3
)
Values
(
@Field1,
@value1,
@value2)
)
-
Dec 9th, 2008, 03:46 AM
#3
Thread Starter
Addicted Member
Re: INSERT with Multiple Queries
when I tried to use this code:
Code:
SET @varNames = SELECT NameID FROM tblNames WHERE Name = @Name
This message appears at the bottom.
Code:
Msg 156, Level 15, State 1, Procedure prPODataEntry, Line 47
Incorrect syntax near the keyword 'SELECT'.
-
Dec 9th, 2008, 05:40 AM
#4
Re: INSERT with Multiple Queries
change it to -
Code:
SELECT @varNames = NameID FROM tblNames WHERE Name = @Name
Please Mark your Thread "Resolved",  if the query is solved & Rate those who have helped you
-
Dec 9th, 2008, 06:19 AM
#5
Thread Starter
Addicted Member
Re: INSERT with Multiple Queries
Thanks guys,
and also I have learned that we can also write it like this:
Code:
SET @varNames = (SELECT NameID FROM tblNames WHERE Name = @Name)
-
Dec 11th, 2008, 11:33 PM
#6
Fanatic Member
Re: INSERT with Multiple Queries
or if u want to insert multiple value to single field, u can use something like this
Code:
INSERT INTO tblTable1
(
Field1
)
SELECT @Field1
UNION
SELECT FieldID FROM tblTable2 WHERE FieldName = @FieldName
UNION
SELECT FieldID FROM tblTable3 WHERE FieldName = @FieldName
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|