Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can this query be written with out using variable?

Tags:

sql

sql-server

Can this query be written with out using variable?

Basically what I need is an ID of some thing from some table for inserting a new record. Right now I'm doing it by storing that ID in variable and then using it.

DECLARE @store_num char(4);
SELECT  @store_num = [store_no] FROM store WHERE (store_name = 'Rocky Mountain Produce');
INSERT INTO [ITD640_B].[dbo].[employee]
           ([employee_no]
           ,[employee_fname]
           ,[employee_lname]
           ,[store_no])
     VALUES
           (123456
           ,'YourFirstName'
           ,'YourLastName'
           ,@store_num);
like image 409
claws Avatar asked Jan 21 '26 08:01

claws


1 Answers

INSERT INTO [ITD640_B].[dbo].[employee]
     ([employee_no]
     ,[employee_fname]
     ,[employee_lname]
     ,[store_no])
SELECT 
     123456
     ,'YourFirstName'
     ,'YourLastName'
     ,[store_no]
FROM 
     store 
WHERE 
     store_name = 'Rocky Mountain Produce';
like image 64
Alex Avatar answered Jan 23 '26 00:01

Alex