SBB
SBB

Reputation: 8970

TSQL Query Assistance

What is the best way to achive this? I am not very good with joins and I think thats what It will have to be in this case. Just need some assistance on this.

I have 2 tables, an employee table and a records table. My query is going to grab a single row from table 1 and insert it into table 2. The question is, how can I do this and also pass variables?

I thought about doing this:

INSERT INTO table 2
(
 col1
,col2
)
SELECT 
  col1
 ,col2
FROM table 1
WHERE user = col1

But I also need to add variables that are not in the table but things I have passed to the stored procedure.

I think it will have to be a join but not sure how to join and insert with the data I got back.

Table 1
-Col 1
-Col 2
-Col 3
-Col 4
-Col 5


Table 2
-Col 1
-Col 2
-Col 3
-Col 4

Upvotes: 0

Views: 52

Answers (1)

htxryan
htxryan

Reputation: 2961

You can simply use the variables. You may be over-thinking it.

INSERT INTO table 2
(
 col1
,col2
,col3
)
SELECT 
 col1
 ,col2
 ,@MyVariable
FROM table 1
WHERE user = col1

EDIT: Removed variable declaration, as the OP noted that the variable is a stored proc parameter (thanks for pointing this out, @Sonam).

Upvotes: 1

Related Questions