Sunday, February 19, 2012

Help with JOINS in Stored Procedures

I am making a stored procedure, and I need to set some variables based on the results of a JOIN statement.

If I something like:

SELECT table.field, table.field2 FROM table INNER JOIN table2 ON table.field = string

How could I get the results and set them in variables?

Any help would be greatly appreciated.

GKC

Are you trying to do something like this?

CREATE PROCEDURE dbo.TestProcedure
(
@.SomeString varchar(50)
)
AS
SET NOCOUNT ON
SELECT t1.Field1, t1.Field2
FROM Table AS t1
INNER JOIN Table2 AS t2 ON t2.Field = t1.Field1
WHERE t1.Field1 = @.SomeString

HTH,
Ryan

|||No, basically I want to do this:

SELECT field1, field2, field3
FROM first_table
INNER JOIN second_table
ON first_table.keyfield = second_table.foreign_keyfield

And take the results and put set them in some variables|||

DECLARE @.f1 ...
DECLARE @.f2 ...
DECLARE @.f3 ...

SELECT @.f1=field1, @.f2=field2, @.f3=field3
FROM first_table
INNER JOIN second_table
ON first_table.keyfield = second_table.foreign_keyfield

|||

GENIUS! So simple, I never thought!

Thank you!

No comments:

Post a Comment