Solved

Paramter to a script?

Posted on 2014-10-28
2
90 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 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 50

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

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 post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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 …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

728 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