?
Solved

Paramter to a script?

Posted on 2014-10-28
2
Medium Priority
?
91 Views
Last Modified: 2014-10-31
Hello,

I have a script that i use to generate the tables and views in two databases.  In the script there is a view that has a join over tables in both databases.

The databases can have different names each time i run the script so this affects the database names in the views.

Is there anyway i can have parameters to a script so i can make it more generic?

Otherwise i must edit the script each time and there is a risk i make a mistake.
0
Comment
Question by:soozh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 1000 total points
ID: 40408176
You can use NVARCHAR like below:
DECLARE @query NVARCHAR(300);
DECLARE @view1 NVARCHAR(128)
DECLARE @view2 NVARCHAR(128)

set @view1='view1_name'
set @view2='view2_name'

SET @query = N' select * from  ' + @view1 + ' union all  select * from ' + @view2 ;

EXEC sp_executesql @query ;

Open in new window

0
 
LVL 51

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 1000 total points
ID: 40408762
Create two variables in the top of the script, to store the database names, so you'll only has to change in one place.
Then work only with those variables in that View section.
0

Featured Post

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question