[SQL] : Declare variable in select query

Hello Experts,

I have the following request :


Open in new window

I would like to optimized the query in order to declare variables
Set @Server=’’
Set @Database = ‘’
Set @Schema = ‘’
Set @View = ‘’

Thank you very much for your help
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark WillsTopic AdvisorCommented:
Would you mind taking a bit of time to explain your requirement ?

While willing and wanting to help... Not quite sure of how to help
LD16Author Commented:

I have a SELECT query which select a view through a linked server.
The thing is that everytime I forgot to change the server in the select query this is why I would like to declare as variables, the linked server, database and view in order to save time.

Example :
select * from [D-P-DWH-SQL].[CubeDWH].ods.Reference_View with (nolock)
Is the used query.

 Now I would like to compare with the same view which is available in other linked server such as

Instead of changing manually the request I can change the variable declared at the beginning.

Thank you very much for your help.
Mark WillsTopic AdvisorCommented:
Ok, thanks, that is clearer...

A bit of dynamic SQL
declare @sql varchar(8000)
set @sql = 'SELECT * FROM '+@SERVER+'.'+@DATABASE+'.'+@SCHEMA+'.'+@VIEW
exec (@sql)

Open in new window

Now, you ideally create a stored procedure so you can pass in the parameters. In doing so you can check and add the square brackets (if needed) and other checks. You can also capture the results...

So what you would end up with is something lik
exec usp_my_stored_procedure @server='[D-P-DWH-SQL]', @database = '[CubeDWH]', @schema='ods', @view= 'Reference_View' 

Open in new window

Does that sound like the right direction ?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

LD16Author Commented:
Yes, this seems to go in the right direction instead of executing a store procedure is not a way to just perform a dynamic select with the declared variables?
Mark WillsTopic AdvisorCommented:
Yes, the Dynamic SQL is just a SQL script and dont need a stored procedure....
LD16Author Commented:
Thank you very much for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.