0

Is something like this possible?:

DECLARE @test nvarchar(30) = 'Appt'

SELECT * FROM @test.dbo.tablename with (NOLOCK)

or possibly

DECLARE @test nvarchar(30) = 'Appt'

SELECT * FROM @test + '.dbo.tablename' with (NOLOCK)    

No, right?

I'm not looking to put the whole string into a variable and then use "EXEC." I have long scripts that I want to just try to replace that with when it occurs.

Thank you.

8
  • 3
    no. not possible. you need to build a string and then exec it. Commented Oct 14, 2016 at 18:27
  • Ok, That's what I thought. Was just being hopeful :( Commented Oct 14, 2016 at 18:28
  • But what is the problem? You can create a function DoQuery(@test) and encapsulate the EXEC call Commented Oct 14, 2016 at 18:29
  • Often, it's easier to take your existing long script, add in placeholders for whatever you want to replace, and then call replace on those strings to fill in any values than to dynamically assemble the whole thing. It sounds like your concern is primarily with ease of development/ maintenance. If you have a string select * from ##dbname##.dbo.tablename with (nolock), you can do replace(your_string, '##dbname##, 'Appt') and then execute that. If you are just replacing database names, maybe you just need sp_MSforeachDB. Commented Oct 14, 2016 at 18:33
  • You might require to go for Dynamic sql Commented Oct 14, 2016 at 18:33

2 Answers 2

2

This requires Dynamic SQL. Basically you create a string that builds the SQL statement dynamically based on your query. That string is then executed with an EXEC Statement. A basic example is like this:

DECLARE @test nvarchar(30) = 'Appt'
DECLARE @sql as varchar(max)
SET @SQL = 'SELECT * FROM' +  @test + '.dbo.tablename' + 'with (NOLOCK)'
EXEC @SQL

Assuming your tablename was Appt.dbo.tablename

I think you meant:

SET @SQL = 'SELECT * FROM' +  'dbo.' + @test + ' with (NOLOCK)'
Sign up to request clarification or add additional context in comments.

3 Comments

Well the problem is that i have many many insert statements and select statements. so i cant put them all into a variable.
You can write your heart away. You simply use SELECT statements and concatenation to create the whole statement and exec at the end. Don't forget scope. Its more challenging to create a variable within the dynamic SQL. Also keep in mind when you execute a statement: SELECT * FROM ABCD INSERT INTO dbo.Something... That will execute together even if there are no spaces.
The statements in your question are open to SQL Injection Attacks. Always use QUOTENAME for table names when building dynamic SQL to avoid such attacks. Like SET @SQL = 'SELECT * FROM' + 'dbo.' + QUOTENAME(@test) + ' with (NOLOCK)'. Also: EXEC (@SQL); (cf msdn.microsoft.com/en-us/library/ms188332.aspx)
1

Nice explanation logixologist.

Because Tom wants to run it for multiple select statements, i was thinking he could store all the table names in a column and use a cursor to fetch the table values. Considering you would create a table first such as:

CREATE TABLE tableName ( name varchar(20));

then run the following code once you inserted the right values in the table above.

Declare @table VARCHAR(255)
DECLARE @sql as varchar(max)
SET @SQL = 'SELECT * FROM' +  'dbo.' + @table + ' with (NOLOCK)'

DECLARE table_cursor CURSOR FOR 
select distinct([name]) FROM DBname.dbo.tableName
Where [name] is not null

OPEN table_cursor
FETCH Next from table_cursor 
INTO @table
WHILE @@FETCH_STATUS =0
BEGIN

EXEC @SQL

FETCH NEXT FROM table_cursor
INTO @table
END
CLOSE table_cursor
DEALLOCATE table_cursor

The cursor would return the table name for the select statement. I think you could use the same logic for insert statements.

1 Comment

Most DBA's would get heartburn if you even mentioned using a Cursor to create Dynamic SQL. It might work but its less maintainable if you run into an issue. Harder to figure out where the problem is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.