[SQL] : Declare variable in select query

Hello Experts,

I have the following request :

SELECT * FROM [SERVER].[DATABASE].[SCHEMA].[VIEW] 

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
LVL 1
LD16Asked:
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
0
LD16Author Commented:
Ok,

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
[D-Q-DWH-SQL]

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

Thank you very much for your help.
0
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 ?
0

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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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?
0
Mark WillsTopic AdvisorCommented:
Yes, the Dynamic SQL is just a SQL script and dont need a stored procedure....
0
LD16Author Commented:
Thank you very much for your help.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.