Solved

Paramter to a script?

Posted on 2014-10-28
2
84 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
Comment Utility
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 45

Assisted Solution

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now