changing database name in sql server script - sql

I created a schema script for an sql server database called test.
I want to execute this script in the same server where the test database is found, but for sure with different name, suppose test2.
when I opened the scripts, it starts by CREATE DATABASE [test] and the name test is used many times in the script.
so how to safely change database name in the script without affecting the original database?
Note: changing name by just replacing it's name is not a solution because it's name is a part of many procedures and functions

No need to use database name in each and every query. Just use
USE [Database_Name]
in the above of the script file, then it will consider the database for the entire script until you specify another database name inside the script.
Eg:-
USE My_Database_1
Script_1
Script_2
.
.
Script_n
--Another Database if required
USE My_Database_2
Script_1
Script_2
.
.
Script_n

Related

How to create a database, table and Insert data into it and use it as a source in another data flow in SSIS?

I have a need to create a SQL database and a table and Insert data into the table from another SQL database . And also to use this newly created database as a oledb source in another dataflow in the same SSIS package. The table and database name are fixed.
I tried using script task to create database and tables. But when I have to insert data , I am not able to give database name in the connection manager as the database is created only in runtime.
I have tried setting ValidExternalMetaData to false, but that doesnt seems to help as well.
Any idea or suggestions on how to accomplish this will be of great help. Thanks
I think you just need two things to make this work:
While developing the package, the database and table will need to exist.
Set DelayValidation to true on the connection manager and dataflow tasks in order to avoid failures with connection tests before they are created.
use a variable to hold the new table name create and populate the using the variable then use the variable name in the source object.

SSIS: How do I create tables using Foreach Loop Container?

I got a bunch of .csv files each containing a script that would create a certain table.
I want to create tables using these scripts in said files (each table to be created using one file).
I got a foreach loop container that specifies the path and which files to use.
I don't know how to configure the Execute SQL Task to execute the script in each one of these files in order to create a table.
You can use the Execute SQL Task with an input parameter of the table name (I would use the table name that the 'for each' container provides. I would first drop the table if it exists and then recreate it with a create table command (in the Execute SQL Task).
As other people have noted you may want to be careful with tasks that drop tables but I have created plenty of SSIS packages that involve truncating and/or creating tables.

create alias for database name within same server

We have three database on same server (dev, test and uat). I am using a fourth database to perform some operations. I have views and stored proc created which utilizes the dev db. When I want to promote the code, I need to change the db name in all views and stored proc. Is there a better way of doing this? We are constrained with single server for all three environment.
Thanks
shankara Narayanan
Always script everything. Then you have a nice .SQL file that you can manipulate in whatever way is necessary. I prefer to set the all up with DROP/CREATE pairs for every view, SP and function. If any of them need to change, i update the script and rerun the whole thing.
I usually use a separate script file for the tables.

Sql server 2008 best way to turn hard coded references to schema name into a variable

We have a database that has been cobbled together over the years. When I export it as a.sql file script even with the options to explicitly refer to the schema name removed a lot of stored procedures use the hard coded schema name [EpicDB].
I have a small powershell utility that is table to reconstruct a versioned database by running the various .sql files we have to make the db in order. Some of these files have hundreds of references to [EpicDB].
How can I pass a variable name from my powershell/.net code to an sql script to swap out [EpicDB] for a variable?
sqlcmd.exe and "SqlCmd Mode" allows for variables.
You'll have to change your
[EpicDB]
text
to something like
[$(MyDatabaseName)]
See:
http://technet.microsoft.com/en-us/library/ms188714.aspx

SQL Server Script Database with Seed Data

I am creating an install script for a Sql server 2008 database using Tasks =>generate Scripts option. This works fine and creates a script including database,schema and seed data.
One problem I notice is that the A Stored procedure is created before the table it refers to is created and this gives error when creating the database.
I don't think there's any built-in functionality for ordering the scripting, but you could split them up in separate scripts and control the order in which those are executed yourself.
For example, select just the tables and generate a tables create script, then select the sprocs and generate another script.