Solved

Paramter to a script?

Posted on 2014-10-28
2
87 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
2 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 250 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 47

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 250 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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