Solved

Paramter to a script?

Posted on 2014-10-28
2
86 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 46

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Changing multiple SQL login passwords 9 31
testing sql16 on win10 vs OS16 2 35
subtract 1 in Access 2003 query 7 37
Join multiple pivot queries 2 8
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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

25 Experts available now in Live!

Get 1:1 Help Now