Dynamically Choose Database

Jun 21, 2004

I have to write a large data migration script to move data from one SQL Server database to another. Is there any way to dynamically specify the server and database name? I would like to do something like the following, (but this does not seem to work):





Delete From [@ServerName].[@ImportToDatabase].[MyTable]





Any help appreciated

View 2 Replies


ADVERTISEMENT

Choose A Column Dynamically

Mar 3, 2008

I want to choose a column dynamically depending on a session variable.
In other words, each customer has a different price level. so I need to choose the column that corresponds to thier price level in thier customer record
Any help is appreciated.
below is my futile attempt
 
SelectCommand="SELECT cove.CATALOG.ITEM_NO,cove.catalog."& <%#= session("pl") %> & ", cove.CATALOG.DESCRIPTION, cove.CATALOG.DESCRIPTION2, cove.CATALOG.DESCRIPTION3, cove.PRODLINE.DESCRIPTION AS Expr1, cove.CATALOG.GRP, cove.COMPANY.PRL, cove.COMPANY.COMPANY FROM cove.CATALOG INNER JOIN cove.COMPANY ON cove.CATALOG.WEB_STATUS <>'[I]' cove.COMPANY.I LEFT OUTER JOIN cove.PRODLINE ON cove.CATALOG.PRODLINE = cove.PRODLINE.CODE WHERE (cove.COMPANY.COMPANY = @company) AND (cove.CATALOG.GRP <> '[ACC]')">
<SelectParameters>

View 3 Replies View Related

DB Design :: Generate A Script To Choose Only Top 10 Items From Each Table In Database?

Aug 22, 2015

I know how to generate scripts that would contain insert queries of all records in all tables. But how can I limit this to top 10 records only from each table?I can select "Data Only" when generating the script, but it generates insert queries for all data. How can get the script to have only top 10 record insert statements? These top 10 should be ordered by the primary key.

View 4 Replies View Related

How To Get A Database Name Dynamically ?

Aug 13, 2007

How to create a sql statement that dynamically matches a database name in a stored proc. Somthing like this:
Select Table_name
From Information_schema.Tables
Where Table_type = 'BASE TABLE' and Objectproperty (Object_id(Table_name), 'IsMsShipped') = 0
and table_name like 'Item%Master'
and (DATABASENAME=????????????????)

View 6 Replies View Related

How To Get The Database Name Dynamically?

Aug 13, 2007

I need an sql statement that dynamically matches a database name in a stored proc. Here is my attempt



Select Table_name

From Information_schema.Tables

Where Table_type = 'BASE TABLE' and Objectproperty (Object_id(Table_name), 'IsMsShipped') = 0

and table_name like 'Item%'

and (DATABASENAME=????????????????)


Can someone help me out?

Thanks.

View 4 Replies View Related

T-SQL (SS2K8) :: Dynamically Set Database

Jun 26, 2014

A user is wanting to run a query against the current 3 active databases. These change every month on the 1st e.g. Test-06-14. Therefore I want to set up a dynamic query which will always use the current database. I believe I am almost there but I cannot set the USE @DatabaseName dynamically yet.

DECLARE @DB_Name varchar(100)
DECLARE @DatabaseName varchar(100)
DECLARE @Command nvarchar(200)
DECLARE @Command2 nvarchar(200)
DECLARE database_cursor CURSOR FOR

[Code] ....

View 9 Replies View Related

Problem Creating Database Dynamically

Jul 29, 2004

Hi everybody,

I have a problem. When I try to create a database with this code:

strConnection = "server=(local);Integrated Security=SSPI"
objConnection = New SqlConnection(strConnection)
objConnection.Open()
Dim strSQL = "CREATE DATABASE xxxxxx ON PRIMARY"
strSQL += "(Name=test_data, filename = 'xxxxx.mdf', size=3,"
strSQL += "maxsize=5, filegrowth=10%)log on"
strSQL += "(name=mydbb_log, filename='xxxxx.ldf',size=3,"
strSQL += "maxsize=20,filegrowth=1)"
objCommand = New SqlCommand(strSQL, objConnection)
objCommand.ExecuteNonQuery()

it usually works, except when the name of the database (xxxxxx) contains a number in the beginning of the name or if it is all made up completely by numbers. I also converted the database name into a string, but it doesn't work either.

What is the problem?
How can I solve it?


Thanks in advance

View 1 Replies View Related

Selecting Database Dynamically (as A Variable)

Jun 2, 2006

Hey all.

I need to call another database in the same server dynamically; creating a varible off it or somehow. Example:

-- In database DBaseA
declare @dbname as varchar (30)
set @dbname='[DBaseB].[dbo]'
select * from @dbname.[table2]

I know this block of code doesnt work. And i need a way of making the  database DBaseB dynamic.
DBaseA and DBaseB are in the same server. I am using Sql Server 2000.
How do i solve this? Solutions ?

View 1 Replies View Related

Create And Then USE A Dynamically-named Database?

Nov 2, 2006

I have a need to create a database, and then populate it. However, thecode below doesn't work as I hoped it might (it creates the table inthe "master" database, which is Not A Good Thing). I know already(thanks Tony!) that if you use Dynamic SQL for the USE command, thenthe subsequent operations need to be Dynamic SQL as well, which is apity since there are over 11,000 lines of it and I don't really fancydebugging it!Does anyone have a cunning plan? In a nutshell, I would like to beable to:1. Create a new database with a derived name - as in the case below aname based on the year, though it might be month as well.2. Create and populate tables in this new database.These operations would ideally be running from a scheduled job.Any thoughts?TIAEdward====================================USE MASTERDECLARE @DBName VARCHAR(123)SET @DBName = 'MyTest_' + CAST((Year(Getdate())) AS Varchar)if not exists(select dbid from master.dbo.sysdatabases where name =@DBName)exec('CREATE DATABASE ' + @DBName)elseraiserror('Database already exists.',3, 1)EXEC ('USE ' + @DBName)if exists (select * from dbo.sysobjects where id =object_id(N'[dbo].[TestTable]') and OBJECTPROPERTY(id, N'IsUserTable')= 1)drop table [dbo].[TestTable]GOCREATE TABLE [dbo].[TestTable] ([TestID] [int] NOT NULL ,[Description] [varchar] (50) COLLATE Latin1_General_CI_AS NULL) ON [PRIMARY]GO

View 3 Replies View Related

Adding A Column To A Table Dynamically Against A Database?

Oct 3, 2007

I have the folowing databases DB1,DB2,DB3,D4,DB5........
I have to loop through each of the databases and find out if the database has a tablename with the word 'Documents'( like 'tbdocuments' or 'tbemployeedocuments' and so on......)
If the tablename having the word 'Documents' is found in that database i have to add a column named 'IsValid varchar(100)' against that table in  that database and there can be more than 1 'Documents' table in a database.
can someone show me the script to do it?
Thanks.
 
 

View 6 Replies View Related

Dynamically Connecting To Different Database In MSRS 2005

Oct 1, 2007

Hi,

I need to create a MSRS 2005 report which needs to connect to differnt databases at runtime.


1. Can we create a report that connects to different databases at runtime and generate a report.
2. Is there any other way to implement the same using .NET 2.0.

Thanks in advance.

Regards,
Rajeev

View 5 Replies View Related

How To Change Database Context Dynamically In SQL 2000

Oct 4, 2007

I am using Sql Server 2000.
I have about 25+ databases . I want to run a series of commands on each database... how can I change the database context - the current database - dynamically in a loop...
something like this below:



declare @SQLString Nvarchar(1000)

declare @DBName Nvarchar(100)

declare @SQL2 NVARCHAR(1000)

declare @TABLE_NAME NVarchar(100)

While @@FETCH_STATUS=0

Begin

SET @SQL2 = 'USE ' + @DB_NAME + ';GO;ALTER TABLE ' + @TABLE_NAME + ' MODIFY Name varchar(100)'

PRINT @SQL2

EXECUTE SP_EXECUTESQL @SQL2

End




I am getting an error when i run the above commands:
Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'GO'

Can someone give me the correct solution?

View 4 Replies View Related

What Is The Best Way To Dynamically Change Server Or Database Name Inside A Sp?

Jun 8, 2006

To be clear:

You have a store procedure and inside you make Updates or Inserts against another Server. But that name can change and I dont want to change it manually in everyplace.

Per example:

SELECT *
FROM Himalaya.DBName.dbo.tblUserGroup
WHERE fldUserID = 7300

I have several Selects, Updates and Inserts pointing to that server.

What I can do if I want to change from Himalaya server to another server?

The same with the Database Name.

Thank you

View 6 Replies View Related

Dynamically Specify Server And Database In Stored Procedure

Feb 28, 2007

I am writing Stored Procedures on our SQL 2005 server that will link with data from an external SQL 2000 server. I have the linked server set up properly, and I have the Stored Procedures working properly. My problem is that to get this to work I am hardcoding the server.database names. I need to know how to dynamically specify the server.database so that when I go live I don't have to recompile all of my stored procedures with the production server and database name. Does anyone have any idea how to do this?

EXAMPLE:

SELECT field1, field2 FROM mytable LEFT OUTER JOIN otherserver.otherdatabase.dbo.othertable

OBJECTIVE:

Replace 'otherserver.otherdatabase.dbo.othertable' with some other process (dbo.fnGetTable('dbo.othertable')????)


Thanks for any help

View 1 Replies View Related

How To Dynamically Assign Database Name In Query Or Store Procedure?

Sep 22, 2006

Hello,

I am not sure if this possible, but I have store procedures that access to multiple databases, therefore I currently have to hardcode my database name in the queries. The problem start when I move my store procedures into the production, and the database name in production is different. I have to go through all my store procedures and rename the DBname. I am just wonder if there is way that I could define my database name as a global variable and then use that variable as my DB name instead of hardcode them?

something like

Declare @MyDatabaseName varchar(30)

set @MyDatabaseName = "MyDB"

SELECT * from MyDatabaseName.dbo.MyTable

Any suggestion? Please.

Thanks in advance

View 8 Replies View Related

Writing Query To Dynamically Select A Database Table

Sep 24, 2006

Is there a way I can write a query to dynamically select a database table. That is, instead of having a variable for a certain column like customerId which would be €œcustomerID = @customerID€? I want to use the variable to select the table. I assume I may have to use a stored procedure but I am unclear as to how this would be coded.

Thanks

View 1 Replies View Related

How To Insert The Value From The Text Box (ASP.NET 2.0) To The Microsoft Sql Server 2000 Database - Dynamically.

Nov 16, 2006

Hello Friends,

I have a problem with ASP.net with dynamic data transfer from asp page to microsoft sql server 2000. For example , I have asp

web page with one text field and a buttion.

When I click the buttion, the value entered in the text field should be transfered from the text field to database. 



Kindly See the following section c# code .....



SqlConnection objconnect = new SqlConnection(connectionString);

SqlCommand cmd = new SqlCommand();
cmd.Connection = objconnect;
cmd.CommandText = " select * from Table_Age where Age = @Age";
cmd.CommandType = CommandType.Text;

SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@Age";
parameter.SqlDbType = SqlDbType.VarChar;
parameter.Direction = ParameterDirection.Output;
parameter.Value = TextBox1.Text;

objconnect.Open(); // Add the parameter to the Parameters collection.
cmd.Parameters.Add(parameter);
SqlDataReader reader = cmd.ExecuteReader();

********************this section c# code is entered under the button control************************************

connection string is mentioned in the web.config file.

In the above c# code , I have a text field , where I entered the age , the value entered in the text field should be sent to

database. I have used SqlParameter for selecting the type of data should be sent from ASP.NET 2.0 to the microsoft sql server 2000. I am facing a problem

where I am unable to sent the random datas from a text field to the database server.


I have a created a database file in the microsoft server 2000.after the excution of the fIeld  value is assinged to NULL in the main database i.e microsoft the sql server 2000.Can anyone help with this issue.My mail id phijop@hotmail.com- PHIJO MATHEW PHILIP.     

View 1 Replies View Related

SQL 2012 :: Dynamically Create Connection To A Database Within SSIS Package

Aug 6, 2015

I am trying to dynamically create the connection to a database within an SSIS package.

the requirement is to allow the user to pass through the database as a variable and that variable will dynamically create the connection string in the connection manager.

Is this possible, if so how?

View 0 Replies View Related

Transact SQL :: Dynamically Creating And Running A Database Restore Command

Jun 17, 2015

If I run the following command in a Query window it works:

RESTORE DATABASE CIS_Source_Data_Test FROM DISK = 'y:CIS_Source_Data_backup_2015_06_17_085557_7782407.bak' WITH RECOVERY, REPLACE

If I dynamically put together the command and store it in variable @cmd and then execute it using
exec sp_executesql @cmd or exec (@cmd) it does not work. I get the following:

Msg 2745, Level 16, State 2, Procedure CIS_Source_Data_Refresh, Line 92
Process ID 62 has raised user error 50000, severity 20. SQL Server is terminating this process.
Msg 50000, Level 20, State 1, Procedure CIS_Source_Data_Refresh, Line 92
RESTORE DATABASE is terminating abnormally.
Msg 0, Level 20, State 0, Line 0

A severe error occurred on the current command.  The results, if any, should be discarded.

Why it won't work when I try to create and run it dynamically?

View 8 Replies View Related

Integration Services :: Database Creation Dynamically In Server From SSIS

Jul 31, 2015

I need to create a DB dynamically in the Server from the SSIS only....

How to implement this....

View 4 Replies View Related

Using SSIS Package To Dynamically Load Data From Database Into Three Separate Flat File

Jul 24, 2015

I have three tables in data base:

customer
product
sales

And i want to use SSIS package dynamically load data from database into three separate flat file, each table into each file.

I know i got to use for each loop task ADO.Net schema row set enumerator, with OLEDB connection manager, select table name or view name variable from access mode list, but the problem comes, as table name is dynamic then flat file connection is also dynamic, i am using visual studio 2013...

View 5 Replies View Related

Which To Choose....

Oct 24, 2007

I have a production server log shipping to a secondary server every 30 minutes (both SQL 2000), which the second server is used for both a warm standby server and for reporting from users. Issue: the log shipping locks the DB so reporting can't be done until the load is finished, the load to the second set of databases has taken up to 15 minutes to finish allowing the users only 15 minutes to run reports, this is not acceptable. The server also needs to be used for DR.

I am looking for another solution, I can't use Transactional Log shipping as not all of the tables in the databases have a primary key identified. So, I am looking for a real-time or near real-time reporting server that is more available to running reports and a warm standby server for Disaster recovery. I am trying to figure out what SQL Server 2000 has to provide (or even 2005 or 2008?) or I am also looking at some third party software, but not sure what is the best for a reasonable price.

Any help is appreciated.

Thanks....JB

View 8 Replies View Related

How To Choose A Primary Key

Feb 16, 2005

Hi All,

I have a dilemn:
On one side, I have a column C1 which could be a primary key because it is never null, the value is unique and identify the record. The problem is its a char type and its lenght can be close to 30.
Then, I've planned to add another column C2 of int type as PK. But then I need to add a unique constraint index on C1. Does it improve performance anyway?

Thanks

View 14 Replies View Related

Index - Which 1 To Choose ?

Jul 6, 2006

good day, everyone

if i have a transaction table with fields below :

transaction_no, product_id, product_desc, product_qty, product_txn, transaction_date

can some expert here point out to me , which is the best cluster-index and non-clsuter index ?

and possible kindly please explain why is it so? i'm not good in database so just explain like to beginner

thank you very much for guidance

View 3 Replies View Related

Choose Two Values (out Of Four Possible)

May 6, 2008

(Hard to put a good subject on this one...)

I have a database containing a lot of users and these users can have four different kind of telephone numbers connected to them: "Direct phone", "Switchboard", "Cell phone", "Home phone". The phone numbers are stored in a separate table. Some users have 0 phone numbers, some have 1, some have 3 etc.

Now I have to transfer the data to another database with a strict table structure and here the table that contains the user also should contain the users phone number and an alternative phone number, if the user currently has more than one phone number connected.

This means that if for instance we have three or more phone numbers connected to one user, we can maximum transfer two of them. This is not a big issue though...

We have ranked the importance of the phone numbers in the order as I presented them above.
What I do in my T-SQL query is to do a ISNULL() and see if the user has "Direct phone" connected, if not I check for the next type and so on.

Now to my problem! Can anyone give me a suggestion of how to write the code for the extraction of the Alternative phone? What I need to do is to check if there is a "Direct phone" connected to the user, if so I should NOT chose that but the next phone number that I find.

View 7 Replies View Related

Help Me Choose Between Two Designs

May 14, 2008

I'm currently developing an ASP.NET website which is using SQL Server 2005 and I couldn't decide between two table designs and I hope you can give me your opinions

The website is for a school and it'll be used to create tests from questions. The teacher will:
1. Select grade (could be multiple selection)
2. Select class
3. Select subject
The thing is that same question could be used for multiple grades.

Example query: "Get me questions of trigonometry of math from grades 7,8,9"

(Names used instead of ID's to make it more clear)
The first design:

[BigRelationsTable]
ID - QuestionID - GradeID - ClassID - SubjectID
1 - Question123 - Grade7 - Math - Trig
2 - Question123 - Grade8 - Math - Trig
3 - Question123 - Grade9 - Math - Trig

This is a simple design but all of the columns will need indexes because all of them will be used for searching and that makes me think about table performance.

Second design:
[GradeClassRelations]
ID - Grade - Class
1 - 7 - Math
2 - 8 - Math
3 - 9 - Math

[ClassSubjectRelations]
ID - GradeClassRelationsID - SubjectID
1 - GradeClassRelations1 - Trig
2 - GradeClassRelations2 - Trig
3 - GradeClassRelations3 - Trig

[SubjectQuestionRelations]
ID - ClassSubjectRelationsID - QuestionID
1 - ClassSubjectRelations1 - 1
2 - ClassSubjectRelations2 - 1
3 - ClassSubjectRelations3 - 1

This one is more normalised but this time the need of doing multiple joins makes me wonder.

What do you think? Which one should I use? Or if you have any other suggestions I'm all ears

View 2 Replies View Related

Choose Maximum Value Row

Dec 18, 2006

hi guys,

I have 4 columns and 3 rows. Columns are Name, Age, Gender and Weight. I have values entered for each column. I need to pick the highest value of weight if Name, Age, Gender are same and put that into new table. how can i do that?

View 6 Replies View Related

Which Method Should I Choose?!

Jun 7, 2006

Greetings SSIS friends

I want to implement the following query using SSIS Data flow Source component :

SELECT * FROM someTable WHERE someColumn = 'H'

How do I restrict the data coming from my data source? By that I mean how do I apply the WHERE clause in SSIS?! Should I use a conditional Split component?! but that would mean retrieving all records first then adding the split component (not the most efficient method surely).



Any suggestions would be much appreciated.

View 6 Replies View Related

OLE DB Of ODBC - Which One To Choose?

Mar 18, 2008

Hi there,

I have created an SSIS package which pushes data from SQL Server (2005) to Oracle (Version 9.2.0.7.0), but having some issues with setting up the connection objects to Oracle.

I have attempted the connection with both the OLE DB Destination connection in SSIS and also with an ODBC connection, but both come up with the same error "ORA-12154: TNS could not resolve service name".

I am using a 32-bit version of SQL Server 2005 and the Oracle DB is 64-bit. Any ideas?

View 7 Replies View Related

Which Collation To Choose?

Jan 21, 2008

Hi there,

There's a field with Varchar(5) type which contains both alpha and alpha-numeric data. When sorting based on this field 1001 comes before 101. Is there anyway I can sort it appropriately; is there any collation or something which could be used.

Result of the sort:
1001
101
102
1022
103

Thanks,
LOA

View 7 Replies View Related

Sql, Choose All Between DATE1 And DATE2

May 22, 2008

I want this...: 
SELECT [DateEntered], [From], [To], [Company], [Catagory], [Client], [Description], [TotalHours] FROM [JcpowersJobs] WHERE ([From] > 5/20/2008 12:00:00 AM) AND ([To] < 5/21/2008 11:59:59 PM)
Obviously this won't work, but can anyone tell me what WOULD work?

View 8 Replies View Related

Which Tools To Choose For Asp.net 2 And Sql 2005

Mar 22, 2006

Hi,

I'm helping a small company built a new website and intranet in asp.net 2.0 and SQL.

I'm having trouble choosing among vs.net 2005, vwd express, SQL 2005
express or SQL 2005 Developers Edition for the prototype/testing phase.

Any advice?  I want to build production-ready versions with easy
migrations to production, but I don't want them to have to spend too
much $$$ while it's just in alpha and being prototyped.

thanks,
Bruce

View 1 Replies View Related

Huge Select Into.. (Can I Choose Not To Log?)

Dec 12, 2000

I have a job that selects alot of data from one database into another.

Can I choose not to log this operation (doesn't need to be and the log fills up before it's done)

Thanks..

type of code:

Insert into database_1
Select * from database_2

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved