Tag: variables

Declare and set a variable with more than one possible value

Is it possible to set a variable to have more than one value? In the example below, I would like to set the @variable and use store numbers 1,4,7 and 12 And then use the variable in the WHERE statment. The below is purely an example to see if this is possible or not Declare […]

Use variable in query for database name

Is it possible to do something like this: DECLARE @SourceDB VARCHAR(100); SET @SourceDB = [DatabaseName] INSERT INTO CompletedScope ( uidInstanceID , completedScopeID , state , modified ) SELECT uidInstanceID , completedScopeID , state , modified FROM SourceDB.[dbo].CompletedScope; Basically use the declared SourceDB variable in the query.

Storing variables from queries in stored procedures

I have a stored procedure with a username parameter. I want to use one query to grab a userid, then use this variable in further queries. Here’s what I have so far. It compiles OK, but on execution I get an error “Error converting data type varchar to uniqueidentifier.” ALTER PROCEDURE [dbo].[sp_User_delete] @username uniqueidentifier AS […]

Set variable from user selection in SQL Server

I am beginner in SQL Server My query in SAP B1 is DECLARE @FROMDATE AS DATETIME SET @FROMDATE= [%0] DECLARE @TODATE AS DATETIME SET @TODATE= [%1] DECLARE @ACCNUM AS NVARCHAR(100) SET @ACCNUM= [%2] get the account name, number for every transaction of an account SELECT T4.[ACCOUNT] AS ‘A/C NUM’, T4.[ACCTNAME] AS ‘A/C NAME’, T4.[TransId] AS […]

SQL refer to foreign keys with a variable

I have to check a few foreign keys in a loop and need to replace the name of the foreign key field with a variable (@fk_field for example). My query looks like this: SELECT ad.id , ad.@fk_field , a1.@jnktn_field FROM @tblname tbl LEFT JOIN @jnktn_tbl jnktn ON tbl.@fk_field=jnktn.@jnktn_field WHERE ad.FK1 <> isnull(a1.ID,0) Apparently it’s not […]

View SQL Server list of declared variables in SQL batch

Does anyone know if there is a way to view the list of variables that have been declared in a SQL batch? It would be something like this: declare @Variable1 datetime2 select * from [List Of Declared Variables Table] I realize it would be something I would only have access to in the current SQL […]

select statement inside sp containing value SQL server 2008

I’m having a simple issue that I believe solution is under my nose, but still cannot figure it out. I have a stored procedure that expects the user for 2 imputs: @userid int, @subtotal int My stored procedure also needs a value from a table that can be obtained from the following query: SELECT discountid […]

T-SQL Insert – Select in Table variable is very slow

I have a table variable and I am inserting in it some values using the “Insert Into – Select” statement. The select is a combination of few joins, and when it is executed separately it takes 3 seconds. The problem is that the whole code takes 3-4 minutes to executed. I wonder is there a […]

Using a string of quoted values in a variable for a SQL WHERE CLAUSE

The answer escapes me…maybe because it is not possible… Example that works… SELECT * FROM TABLEA WHERE FIELD1 IN (‘aaa’,’bbb’,’ccc’) Example that does not work… Attempt to leverage variable so that I can define the values once in a string of statements DECLARE @ListValues VARCHAR(50) SET @ListValues = ”’aaa”,”bbb”,”ccc”’ SELECT * FROM TABLEA WHERE FIELD1 […]

How Do I Set A variable Inside a Update Procedure before the Update Command in SqlServer?

I am creating a procedure that will calculate declining depreciation for a fixed asset. For this I need to set a value inside the procedure based on a CASE which in turn is based on the values from the tables I use in the Update command. I need to know how to set that variable […]

MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.