Bishoy Ezzat Bishoy Ezzat -4 years ago 94
SQL Question

How to add an update statement value to a variable?

I want to save data in a variable and use it later in a procedure.

UPDATE acc_Account
SET acc_Account.CompanyID = ( SELECT TOP 1
utl_Company.CompanyID
FROM utl_Company
ORDER BY CompanyID DESC
)
WHERE acc_Account.AccountNumber = @AccountNumber


how can I save the CompanyID in a variable to use it in an insert statement later on?

Answer Source

Have this in the beginning of your code:

declare @var varchar(20) -- change the data type according to your needs
set @var = (SELECT TOP 1 utl_Company.CompanyID FROM utl_Company ORDER BY CompanyID DESC)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download