Tasos Tasos - 4 months ago 9
SQL Question

Insert Into a table a variable outside of the SELECT results

I am working on a Stored Procedure on MSSQL. This is a simplified example of my case.

another code that has declared a variable @Country
...
...
...
INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers;


So, what I try to do is instead of the
Country
value of the
SELECT
, insert the
@Country
variable of the previous code, but keep the
SupplierName
from the results.

I wasn't able to find a similar question and trial and error didn't help.

Answer

Just use the variable instead:

Insert Into Customers (CustomerName, Country)
Select SupplierName, @Country 
From Suppliers;
Comments