2

I have a stored procedure:

CREATE PROCEDURE MyProc
(
    @Param1  [datetime],
    @Param2  [nvarchar](20),
    @Param3  [nvarchar](20),
    @Param4  [nvarchar](20),
    @Param5  [nvarchar](20),
)
AS
BEGIN   
INSERT INTO MyTable1
  (
    Field1,
    Field2,
    Field3,
    Field4,
    Field5
  )   
 SELECT         
    @Param1,
    @Param2,
    @Param3,
    Field12,
    'constantValue'         
FROM   MyTable2
WHERE  Field13 = @Param4
END

How could I change the stored proc in order it inserts into the Field5 of MyTable1 not the constantValue but a result of yet another query to MyTable2 with the last parameter (@Param5)?
I.e.:

FROM   MyTable2
WHERE  Field13 = @Param5
3
  • Simply change the constant value to myTable2.fieldname Commented Nov 21, 2011 at 15:59
  • How many rows from the query? 1 or more than 1? Commented Nov 21, 2011 at 16:01
  • I want two values from the same field Field12 but from different records of MyTable2 Commented Nov 21, 2011 at 16:04

4 Answers 4

4
INSERT INTO MyTable1
(
  Field1,
  Field2,
  Field3,
  Field4,
  Field5
)   
SELECT         
    @Param1,
    @Param2,
    @Param3,
    Field12,
    (
         select field_name from MyTable2 where Field13 = @Param5
    )
FROM   
    MyTable2
WHERE 
    Field13 = @Param4
END

Or declare a new variable:

SET @newparam = (SELECT field_name FROM MyTable2 WHERE Field13 = @Param5 )

Then insert this new parameter into the insert statement.

Sign up to request clarification or add additional context in comments.

Comments

3
CREATE PROCEDURE MyProc
(
    @Param1  [datetime],
    @Param2  [nvarchar](20),
    @Param3  [nvarchar](20),
    @Param4  [nvarchar](20),
    @Param5  [nvarchar](20),
)
AS
BEGIN   
INSERT INTO MyTable1
  (
    Field1,
    Field2,
    Field3,
    Field4,
    Field5
  )   
 SELECT         
    @Param1,
    @Param2,
    @Param3,
    Field12,
    (SELECT thing_i_care_about FROM MyTable2 WHERE Field13 = @Param5)
FROM   MyTable2
WHERE  Field13 = @Param4
END

Comments

2

Assumes one row from each SELECT

SELECT         
    @Param1, @Param2, @Param3, Tp4.Field12, Tp5.Field12
    NULL        
FROM
   MyTable2 Tp4
   CROSS JOIN
   MyTable2 Tp5
WHERE
   Tp4.Field13 = @Param4 AND Tp4.Field13 = @Param5

The JOIN changes depending on what you expect eg FULL OUTER JOIN .. ON 1=1

If either query returns more than 1 row then:

  • in-line sub-query fails
  • insert doesn't make sense: what to insert with 4 rows and 3 rows?

Comments

1

It can be accomplish in different ways as below, in both cases I think we should consider a thing that is what if the returned data might be more than one.

a.

INSERT INTO MyTable1
(
 Field1,
 Field2,
 Field3,
 Field4,
 Field5
)   

SELECT         
 @Param1,
 @Param2,
 @Param3,
 Field12,
  (SELECT TOP 1 Column_Name FROM MyTable2 WHERE Field13 = @Param5)
FROM   MyTable2
WHERE  Field13 = @Param4

b.

DECLARE @Field13 VARCHAR(20)
SELECT TOP 1 @Field13  = FROM MyTable2 WHERE Field13 = @Param5

INSERT INTO MyTable1
(
 Field1,
 Field2,
 Field3,
 Field4,
 Field5
)   

SELECT         
 @Param1,
 @Param2,
 @Param3,
 Field12,
 @Field13
FROM   MyTable2
WHERE  Field13 = @Param4

In either cases will be success though "Field13 = @Param5" condition may reveal multiple data but will work on first(TOP 1) data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.