[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
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)
)
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'.
Re: INSERT with Multiple Queries
change it to -
Code:
SELECT @varNames = NameID FROM tblNames WHERE Name = @Name
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)
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