Showing posts with label databases. Show all posts
Showing posts with label databases. Show all posts

Friday, March 30, 2012

Running Analysis Services 2005 on a separate sever

My first question is can I run Analysis Services 2005 on a different server to where the databases are running

If yes, any pointers to some articles.

Hi justSomeone,

You can create an empty Analysis Services database within an instance of Analysis Services, by using either SQL Server Management Studio or Business Intelligence Development Studio, and then connect directly to that database using Business Intelligence Development Studio and create objects within it (rather than within a project). When working with an Analysis Services database in this manner, changes made to objects take effect in the database to which you are connecting when the changed object is saved.

You may find this article is useful to you:http://msdn2.microsoft.com/en-us/library/ms174953.aspx

Also, this one:http://msdn2.microsoft.com/en-us/library/ms174953.aspx gives you a step-by-step guidline on how to configure datasource in Microsoft SQL Server 2005 Analysis Services (SSAS).

Hope my suggestion helps

Wednesday, March 28, 2012

Running a report against multiple databases

We have multiple databases on the same server. When I publish a report to
the Report Server how do I point to the database I want to run the report
against?If you use Report Manager, click on the property tab for the report and then
on the Data Source sub-tab. From here you can enter in all necessary
connection information.
--
-Daniel
This posting is provided "AS IS" with no warranties, and confers no rights.
"Doni G" <DoniG@.discussions.microsoft.com> wrote in message
news:81C1CFCE-7A33-4A3D-AB30-5430EA3EA628@.microsoft.com...
> We have multiple databases on the same server. When I publish a report to
> the Report Server how do I point to the database I want to run the report
> against?

Wednesday, March 21, 2012

run times of SSIS package not making sense....HELP!

We have an SSIS package that was created to migrate data in from a few production databases. The steps for the package are as follows...

    backup databases on server 1 (prod database server) restore database to SSIS server (server 2) . truncate worker tables in SSIS server's (server 2) Main DB database. copy data from restored db tables to working db tables ( database to database) Start Multiple threads (15 ) and run steps from here in parrallel Combination of Data flow tasks and SQL scripts and Stored procedures used to flatten data out and combine data for reporting purposes.

The average run time is 8 hours.

the issue we are seeing is this, the package will fluctuate in run times from 4 hours to over 11 with no change in the data or the underlying SSIS package. We have looked for any changes or things that would effect this but have not found anything that changed...

Also, certain steps are running shorter while others double in time. there doesnt seem to be any rhyme or reason to this behaviour. The server is x64 12GB of RAM 2 dual core 3.2Ghz.

Please let me know if you need any more information or specifics...

the only thing I have seen so far that looks out of place is Tempdb has one of its files that is 20+GB.

Thanks,

Chris

Are you using "fast load" in your OLE DB Destination connections?|||

yes we are using Fast load...

we have narrowed the actual longest running spot right now... it has been running for over 10 hours....

I had to change some of the names of Tables etc... but here is the rought outline of what the SQL task does:

truncate table [Reporting].[dbo].[ReportTable]
INSERT INTO [Reporting].[dbo].[ReportTable] (pid)
SELECT DISTINCT [MainAPPDB].[dbo].[ReportTableHistory].[Client_ID]
FROM [MainAPPDB].[dbo].[ReportTableHistory]
LEFT OUTER JOIN [MainAPPDB].[dbo].[ReportTableStatus] ON [MainAPPDB].[dbo].[ReportTableStatus].[ReportTableStatusID] = [MainAPPDB].[dbo].[ReportTableHistory].[ReportTableStatusID]
LEFT OUTER JOIN [SecondAPPdb].[dbo].[tblBrokerContact] ON [SecondAPPdb].[dbo].[tblBrokerContact].[BrokerContact_ID] = [MainAPPDB].[dbo].[ReportTableHistory].[LastUpdatedBy]

UPDATE [Reporting].[dbo].[ReportTable] SET rdata = X.rAsXML
FROM
(
SELECT
C.pid as 'primid',
(SELECT [MainAPPDB].[dbo].[ReportTableStatus].[Description] --varchar(25)
,CONVERT(CHAR(30),[MainAPPDB].[dbo].[ReportTableHistory].[LastUpdatedOn],100) AS [LastUpdatedOn] --varchar(30)
,[SecondAPPdb].[dbo].[tblContact].[Contact_FName] + ' ' + [SecondAPPdb].[dbo].[tblContact].[Contact_LName] AS [LastUpdatedBy] --varchar(50)
,[MainAPPDB].[dbo].[ReportTableHistory].[Reason] --varchar(300)
FROM [MainAPPDB].[dbo].[ReportTableHistory]
LEFT OUTER JOIN [MainAPPDB].[dbo].[ReportTableStatus] ON [MainAPPDB].[dbo].[ReportTableStatus].[ReportTableStatusID] = [MainAPPDB].[dbo].[ReportTableHistory].[ReportTableStatusID]
LEFT OUTER JOIN [SecondAPPdb].[dbo].[tblContact] ON [SecondAPPdb].[dbo].[tblContact].[Contact_ID] = [MainAPPDB].[dbo].[ReportTableHistory].[LastUpdatedBy]
WHERE [C_ID] = C.pid FOR XML RAW ('ReportTableHistory'), ROOT('ReportTableStatusHistories'), ELEMENTS XSINIL) as rAsXML
FROM [Reporting].[dbo].[ReportTable] C) X
WHERE X.primid = pid

DECLARE @.inxml XML
DECLARE @.res XML
DECLARE @.pid INT

DECLARE cur CURSOR fast_forward FOR
SELECT pid,rdata FROM [Reporting].[dbo].[ReportTable]
OPEN cur
FETCH next FROM cur INTO @.pid, @.inxml
WHILE @.@.fetch_status = 0
BEGIN
EXEC ExternFunctions_FormatReportTableHistory @.inxml, @.res OUT
UPDATE [Reporting].[dbo].[Client] SET ReportTableHistory = @.res
WHERE [Reporting].[dbo].[Client].[Client_ID] = @.pid
FETCH next FROM cur INTO @.pid,@.inxml
END
CLOSE cur
DEALLOCATE cur

|||Try asking this question in the Transact-SQL forum as it seems that's where your issue is. It doesn't look like this is an SSIS issue anymore.

http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=85&SiteID=1|||

ok I posted the question there as well..

Run SQL T-SQL Job Step across Different Domains

I have two sql 2005 servers in two different domains. I have a SQL job on
one of the servers that runs a T-SQL script that accesses databases on the
SQL box inthe other domain.
I'm getting a message in the job history that the job won't run because of
the security context.
How can I set this up?
Thanks for your help.
--
MWDWhat Operating Systems are these two servers running?
What trusts, if any, are set up between the two domains?
How are you trying to connect?
What is the exact error number and error message?
-Sue
On Thu, 9 Aug 2007 08:10:09 -0700, MWD
<MWD@.discussions.microsoft.com> wrote:

>I have two sql 2005 servers in two different domains. I have a SQL job on
>one of the servers that runs a T-SQL script that accesses databases on the
>SQL box inthe other domain.
>I'm getting a message in the job history that the job won't run because of
>the security context.
>How can I set this up?
>Thanks for your help.sql

Tuesday, March 20, 2012

run sql query on multiple databases

I need to run a same query on 15 different databases and union the results.

Does any of the experts know how to do this with good performance?

Any help is appreciated.

Phil....the only reason i posted in T-Sql is because i will be using this query in reporting services :)

Thanks

Nice to see you over here...

The only reason I invited you over here is that using the power of SSIS may be better suited than doing a large cross-database query. Here's what I'd suggest...

Build a package in SSIS that connects to all of your various databases, and then using the union all component, you can store the results in a staging table to be used in SSRS. Or do you need the results real-time?|||The results will be updated every week.|||

RookieDBA wrote:

The results will be updated every week.

There you go... Using concepts from the data warehouse world, this is your best bet anyhow. You're performance may be faster obtaining the results through SSIS (versus transact-sql -- only you can test that), but certainly when users run reports in SSRS against this "reporting" table, they'll be most appreciative that they aren't waiting for data to be assembled.|||If all source tables have same estructure; you could have a dataflow with only one Source/destination component and then call the package 15 times; passing each time a difrent connection for the source component; that would help incase more sources are added down the road...|||

Yeah its fifteen different databases with five same tables and same sql code.

I will try using SSIS and see the performance.

Thanks

|||

Rafael, could you tell me what toolbox item to use for "call the package 15 times, passing each time a different connection for the source..........."

Thanks

|||

There is not an specific task for that. You would need to create an extra package; a master package that will call the child package that performs the extract/load. The trick would be to use a ForEach loop that iterates through the 15 set of connections and put the connection strings in variables (they can be in a table/file etc); inside of the ForEach Loop you will have an Execute Package task. Then the child package would need to use package configuration (from parent variable) to get the proper connection string on each iteration.

Does this make sense?

The only problem I see with this approach, it is that each iteration has to wait until the previous one is completed; so parallelization is not possible. I used a different approach to achieve parallel execution some time ago; but to be honest I did not like it at all; as you have to use a execute package task for every single execution you need (in your case would be 15!). I explained here in case you want to take a look:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=816000&SiteID=1

Run Sql on multiple databases

I need to run a same query on 15 different databases and union the results.

Does any of the experts know how to do this with good performance?

Any help is appreciated.

Thanks

Come on over to the SSIS forum and we'll be glad to help you out with this.|||

What are you trying to do? You may get adequate performance just by executing the query:

select column1, column2
from database1.schema.table
union all
select column1, column2
from database2.schema.table
...

union all
select column1, column2
from databaseN.schema.table

The key is:

1. RAM - enough to work with the set in memory, if possible
2. Query plans - one slow query will run much slower than 15 fast ones (I just built a 26 query join on 15 million rows that runs in < 1 second.))

Run sp when job fails

I have a few backup jobs of SQL 2000 databases that are scheduled and run
via SQL Agent. Is it possible to have SQL Server run a stored procedure if
a job fails? I only saw options for emailing but that required mail
profiles, etc. Thanks.
David
>I have a few backup jobs of SQL 2000 databases that are scheduled and run
>via SQL Agent. Is it possible to have SQL Server run a stored procedure if
>a job fails?
Sure, create the job with two steps. Step 1 does the bulk of your work. It
is set to quit the job with success, or move to the next step on failure.
The next step (which only gets reached if step 1 fails) can call your stored
procedure, send an e-mail, etc.
A

Run sp when job fails

I have a few backup jobs of SQL 2000 databases that are scheduled and run
via SQL Agent. Is it possible to have SQL Server run a stored procedure if
a job fails? I only saw options for emailing but that required mail
profiles, etc. Thanks.
David>I have a few backup jobs of SQL 2000 databases that are scheduled and run
>via SQL Agent. Is it possible to have SQL Server run a stored procedure if
>a job fails?
Sure, create the job with two steps. Step 1 does the bulk of your work. It
is set to quit the job with success, or move to the next step on failure.
The next step (which only gets reached if step 1 fails) can call your stored
procedure, send an e-mail, etc.
A

Run sp when job fails

I have a few backup jobs of SQL 2000 databases that are scheduled and run
via SQL Agent. Is it possible to have SQL Server run a stored procedure if
a job fails? I only saw options for emailing but that required mail
profiles, etc. Thanks.
David>I have a few backup jobs of SQL 2000 databases that are scheduled and run
>via SQL Agent. Is it possible to have SQL Server run a stored procedure if
>a job fails?
Sure, create the job with two steps. Step 1 does the bulk of your work. It
is set to quit the job with success, or move to the next step on failure.
The next step (which only gets reached if step 1 fails) can call your stored
procedure, send an e-mail, etc.
A

Run scripts on multiple servers in SQL Management Studio

I have a number of scripts that I need to run on multiple servers and databases. Once I have created a Solution with them all in, how do I make them run in a specific sequence, and how can I switch the focus of the entire set of scripts to the other databases I want to run them on?

Thanks,

-Rob

There is no way to do this included in SQL Management Studio. Solutions in SMS aren't like VS solutions, in that you can execute code pages and objects. In SMS the solution is just a convenient place to store and organize the script files you are working with.

You would have to open each script seperately and execute it against the server and database you wanted in the order you wanted.

Sorry, it's going to be a manual process. There are some 3rd party tools that might help some. Check Red Gate and APEX products.

|||

You can do this by using a batch file to execute either OSQL or SQLCMD (preferred) - both of which can be used to execute script files. If you pass server and database names as parameters into your batch file you can then pass the parameter values to OSQL or SQLCMD.

I don't have any examples to hand, but the following links should set you on the right track:

http://msdn2.microsoft.com/en-us/library/ms162806.aspx

http://msdn2.microsoft.com/en-us/library/ms162773.aspx

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true

This is how we deploy changes to our databases in each of our environments - the only thing that changes between each environment is the server name parameter. We generally have one batch file per database and one SQL script per database object - one batch file can be used to execute hundreds of SQL script files if need be.

Chris

|||

Dan and Chris,

Thanks for your response on my question. Not the answers I was hoping to get, but thanks anyway!! I have used the DOS bat file method successfully in the the past, but it is a bit clunky and was hoping against hope that there would be a better method in the SQL management Studio. DO you think I may be looking in the wrong tool?

I can't believe that I am the only one who thinks that the lack of a tool or process to set up implementation of scripts is a serious gap in the SQL Management Studio.

Thanks again, Rob

|||

You have to remember that SQL Server is an RDBMS. Tools provide for development are extras that I find invaluable. Just look at how clunky the management and development tools that come with Oracle have been over the years.

On a good note, you may be able to make some progress by developing your procedures and sql code in Visual Studio instead of SMS. If you have VS 2005 professional edition you can create a sql server database project that will allow you to write t-sql code and execute it. You should be able to write a minor application that would execute the scripts for you against a list of databases and/or servers.

Sorry that there isn't an easier way to do it through SMS. Perhaps it will be a forthcoming feature. I'll suggest it on the product feedback group.

Run scripts on multiple servers in SQL Management Studio

I have a number of scripts that I need to run on multiple servers and databases. Once I have created a Solution with them all in, how do I make them run in a specific sequence, and how can I switch the focus of the entire set of scripts to the other databases I want to run them on?

Thanks,

-Rob

There is no way to do this included in SQL Management Studio. Solutions in SMS aren't like VS solutions, in that you can execute code pages and objects. In SMS the solution is just a convenient place to store and organize the script files you are working with.

You would have to open each script seperately and execute it against the server and database you wanted in the order you wanted.

Sorry, it's going to be a manual process. There are some 3rd party tools that might help some. Check Red Gate and APEX products.

|||

You can do this by using a batch file to execute either OSQL or SQLCMD (preferred) - both of which can be used to execute script files. If you pass server and database names as parameters into your batch file you can then pass the parameter values to OSQL or SQLCMD.

I don't have any examples to hand, but the following links should set you on the right track:

http://msdn2.microsoft.com/en-us/library/ms162806.aspx

http://msdn2.microsoft.com/en-us/library/ms162773.aspx

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true

This is how we deploy changes to our databases in each of our environments - the only thing that changes between each environment is the server name parameter. We generally have one batch file per database and one SQL script per database object - one batch file can be used to execute hundreds of SQL script files if need be.

Chris

|||

Dan and Chris,

Thanks for your response on my question. Not the answers I was hoping to get, but thanks anyway!! I have used the DOS bat file method successfully in the the past, but it is a bit clunky and was hoping against hope that there would be a better method in the SQL management Studio. DO you think I may be looking in the wrong tool?

I can't believe that I am the only one who thinks that the lack of a tool or process to set up implementation of scripts is a serious gap in the SQL Management Studio.

Thanks again, Rob

|||

You have to remember that SQL Server is an RDBMS. Tools provide for development are extras that I find invaluable. Just look at how clunky the management and development tools that come with Oracle have been over the years.

On a good note, you may be able to make some progress by developing your procedures and sql code in Visual Studio instead of SMS. If you have VS 2005 professional edition you can create a sql server database project that will allow you to write t-sql code and execute it. You should be able to write a minor application that would execute the scripts for you against a list of databases and/or servers.

Sorry that there isn't an easier way to do it through SMS. Perhaps it will be a forthcoming feature. I'll suggest it on the product feedback group.

Run reports on SQL 2000 databases with RS 2005?

All our databases are running on SQL 2000, with no migration to SQL
2005 planned at this time.
We do not currently use Reporting Services. A deployment of Reporting
Services 2005 is in the pipeline.
My question is, will I need to install RS 2005 on a SQL 2000 server to
be able to report on my 2000 databases? Or will it work to install RS
2005 on SQL2005 and report on SQL 2000 databases?
Thanks for helping a neophyte.The box that run RS 2005 must have a SQL Server 2005 license. You can use
SQL 2000 as the object/metadata store or you can use SQL 2005. RS reports
off of many many different types of data (SQL Server 2000, 2005, Sybase,
Oracle, etc). The does not need to and in many cases does not reside on the
same box as RS.
Even when you install RS it does not have to have the database on the same
machine(it must have a database for its object/metadata store, it just does
not have to be on the same machine) However, even if the database resides
elsewhere you have to have a license on whatever box you install RS 2005.
My preference is to have SQL Server 2005 on the box with RS even if it is
only used for RS and all the data being reported off of is elsewhere.
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"xr7" <ewilson@.tpna.com> wrote in message
news:1158165177.262203.310810@.d34g2000cwd.googlegroups.com...
> All our databases are running on SQL 2000, with no migration to SQL
> 2005 planned at this time.
> We do not currently use Reporting Services. A deployment of Reporting
> Services 2005 is in the pipeline.
> My question is, will I need to install RS 2005 on a SQL 2000 server to
> be able to report on my 2000 databases? Or will it work to install RS
> 2005 on SQL2005 and report on SQL 2000 databases?
> Thanks for helping a neophyte.
>|||Yes, this will work:
"Or will it work to install RS 2005 on SQL2005 and report on SQL 2000
databases?"
Steve MunLeeuw
"xr7" <ewilson@.tpna.com> wrote in message
news:1158165177.262203.310810@.d34g2000cwd.googlegroups.com...
> All our databases are running on SQL 2000, with no migration to SQL
> 2005 planned at this time.
> We do not currently use Reporting Services. A deployment of Reporting
> Services 2005 is in the pipeline.
> My question is, will I need to install RS 2005 on a SQL 2000 server to
> be able to report on my 2000 databases? Or will it work to install RS
> 2005 on SQL2005 and report on SQL 2000 databases?
> Thanks for helping a neophyte.
>

Run querys after silent installtion SQL 2005 Express

Hi,

I'm looking for a solution to run querys (create databases ed) after a silent installation of SQL 2005 Express edition (example with a .sql file). So users can run a installation unattended with all databases installed and so on.

What I got is a document about how to run a silent installation (document " Using Command Prompt Options to install SQL Server Express") but this docs there's no solution how to run querys after the installation.

Thanks

Robert-Paul

I think you want to use sqlcmd.exe after installation. This really isn't a setup issue. You will probably get a better answer in either the Tools or DB Engine forums.|||

Moving this post to the new SQL Express forum. Hope they can answer your question.

-Jeffrey

|||

Hi Robert- Paul,

You should use SQLCmd to run .sql scripts after a silent installation. You can find more information about SQLCmd in Books Online, which you can either download or find online at http://msdn2.microsoft.com. In general, you can call SQLCmd the same way you'd call any command line tool with a command line something like this:

SQLCmd -E -S <machinename>\SQLEXPRESS -i C:\myscript.sql

Regards,

Mike Wachal - SQL Express team

Monday, March 12, 2012

run query sp_configure

I downloaded SQL Express 2005 and have used the query tool but cannot create databases or even find the query tool? Am I missing something?

Thanks for any help.

Frank

In order to obtain the 'Client tools', you need to download the SQL Server 2005 Express with Advanced Services edition.

Get it here:

SQL Server 2005 Express Edition (Advanced/SSMS)
http://msdn.microsoft.com/vstudio/express/sql/compare/default.aspx
http://msdn2.microsoft.com/en-us/library/ms365247.aspx
http://tinyurl.com/yelwr9

SQL Server 2005 SSMS Express Tutorial
http://tinyurl.com/nqsca

run query on multiple server

query return list of user or
query return list od databases or
something else
This is fine when I run query on one server.
I must get resault for other server.
How to get information for all servers on the LAN or from txt file with
servers name?
Thanks
TaleHi
If you have a text file with the server names (say servers.txt) you could do
FOR /F %i IN (servers.txt) DO osql -S%i -E -dMaster -Q"SELECT * FROM
Pubs..Authors"
John
"Tale" <tales@.eunet.yu> wrote in message news:e1t5k9$kmn$2@.news.eunet.yu...
> query return list of user or
> query return list od databases or
> something else
> This is fine when I run query on one server.
> I must get resault for other server.
> How to get information for all servers on the LAN or from txt file with
> servers name?
> Thanks
> Tale
>

run query on multiple server

query return list of user or
query return list od databases or
something else
This is fine when I run query on one server.
I must get resault for other server.
How to get information for all servers on the LAN or from txt file with
servers name?
Thanks
TaleHi
If you have a text file with the server names (say servers.txt) you could do
FOR /F %i IN (servers.txt) DO osql -S%i -E -dMaster -Q"SELECT * FROM
Pubs..Authors"
John
"Tale" <tales@.eunet.yu> wrote in message news:e1t5k9$kmn$2@.news.eunet.yu...
> query return list of user or
> query return list od databases or
> something else
> This is fine when I run query on one server.
> I must get resault for other server.
> How to get information for all servers on the LAN or from txt file with
> servers name?
> Thanks
> Tale
>

Run query against multilpe database

I have to run a script I have written against multiple databases (about 40) - how would I do this? Once I have the results I need them all to be unioned into one table.

Part of the script is below...

Thanks for any help.

Smile

Code Snippet

USE db1

SELECT * FROM Users
WHERE (Users.user_type = '1.0' AND Users.user_id <> 'itadmin')
OR (Users.user_id LIKE '%test%' OR Users.user_forename LIKE '%test%' OR Users.user_surname LIKE '%test%')

If the databases are on the same server, fully qualify the field: i.e. databasename.tablename.fieldname

If they are on different servers, you'll need to use sp_addlinkedserver which, if you're not the DBA, you might have access to.

In this case, I normally just port the tables on to a development database usign the Import/Export wizard and discard them when I'm done.

Adamus

|||The problem with this is that the script is really long, and with so many databases, adding the full link to the db is not practical. I am currently trying to use the sp_msforeachdb proc, which seems a bit better, but still not really there yet!
Thanks anyway.
|||Ok, so I've got a bit further... Have managed to get all of the info from all of the databases - but does anyone know how I could union all of the results gained from this piece of code.
Thanks
Smile

declare @.cmd1 varchar(500)
set @.cmd1 =
' if ''?'' NOT IN (''tempdb'')
USE ?
SELECT ''?'',Users.* FROM Users
WHERE (Users.user_type = ''1.0'' AND Users.user_id <> ''itadmin'')
OR (Users.user_id LIKE ''%test%'' OR Users.user_forename LIKE ''%test%'' OR Users.user_surname LIKE
''%test%'')'
exec sp_MSforeachdb @.command1=@.cmd1

|||

Best of luck

Adamus

|||

This seemed like a great thing to do with CROSS APPLY

But, alas, I couldn't find a way to initiate dynamic SQL from within a function

I did come up with this....it's not elegant, but it's functional

Code Snippet

create table #dblist ( dbname varchar(100) )

insert into #dblist

select '[db1]' union all

select '[db2]' union all

select '[db3]' union all

.

.

.

select '[db40]'

DECLARE @.dbname varchar(100),

@.selectfrom nvarchar(20),

@.scriptbody nvarchar(4000),

@.sql nvarchar(200)

--@.selectfrom is the part of the script before the dbname qualifier

SET @.selectfrom = N'select * from '

--@.scriptbody is the .dbo.tablename {rest of script}

SET @.scriptbody = N'.dbo.mytable'

--This table variable holds the results of all the query executions

-- make it a perm table or a temp table or whatever.

DECLARE @.results table ( id int, [name] varchar(100) )

DECLARE db_csr CURSOR FOR

SELECT dbname FROM #dblist --probably make #dblist a permanent table

OPEN db_csr

FETCH NEXT FROM db_csr

INTO @.dbname

WHILE @.@.FETCH_STATUS = 0

BEGIN

PRINT ' Start: ' + @.dbname

PRINT getdate()

--this is the guts

SET @.sql = @.selectfrom + @.dbname + @.scriptbody

INSERT INTO @.results

EXEC (@.sql)

PRINT ' End: ' + @.dbname

PRINT getdate()

FETCH NEXT FROM db_csr

INTO @.dbname

END

CLOSE db_csr

DEALLOCATE db_csr

--============

--review final product

SELECT *

FROM @.results

Let me know if you have any questions.

|||

Here's a version that uses USE DB

plus a couple of variable tweaks

Code Snippet

create table #dblist ( dbname varchar(100) )

insert into #dblist

select '[db1]' union all

select '[db2]' union all

select '[db3]' union all

.

.

.

select '[db40]'

DECLARE @.dbname varchar(100),

@.use nvarchar(20),

@.scriptbody nvarchar(max),

@.sql nvarchar(max)

--@.use is the part of the script before the dbname qualifier

SET @.use = N'use '

--@.scriptbody is the rest of script

SET @.scriptbody = N'; select * from mytable;'

--This table variable holds the results of all the query executions

-- define the columns as you need it

-- make it a perm table or a temp table or whatever.

DECLARE @.results table ( id int, [name] varchar(100) )

DECLARE db_csr CURSOR FOR

SELECT dbname FROM #dblist --probably make #dblist a permanent table

OPEN db_csr

FETCH NEXT FROM db_csr

INTO @.dbname

WHILE @.@.FETCH_STATUS = 0

BEGIN

PRINT ' Start: ' + @.dbname

PRINT getdate()

--this is the guts

SET @.sql = @.use + @.dbname + @.scriptbody

INSERT INTO @.results

EXEC (@.sql)

PRINT ' End: ' + @.dbname

PRINT getdate()

FETCH NEXT FROM db_csr

INTO @.dbname

END

CLOSE db_csr

DEALLOCATE db_csr

--============

--review final product

SELECT *

FROM @.results

|||

Kate

SSIS is designed to handle these types of tasks as well.

There is a FOREACH process you can use as well as accessing various data sources.

|||As Dale indicates, SSIS is most likely the best solution for your problem.|||Can anyone point me in the direction of a good tutorial/explanation of SSIS? I've not used it before with SQL Server.
Cheers
K Smile
|||

You might find that a visit to http://www.sqlis.com/ is worth your time and effort.

Also:

http://msdn2.microsoft.com/en-us/library/ms167031.aspx

http://digg.com/programming/Tutorial_1_Creating_Your_First_SSIS_Package_Using_SQL_Server_2005_Beta_2

|||Thanks for that Arnie - I will look at them now.
K Smile
|||Thanks for this Dale - I don't have SSIS on my PC, so this has been used to create my answer. Smile

Wednesday, March 7, 2012

Run a select from against two dbs.

Hi,
is there any possiblity to run a select statement against the tables of two different databases which are both installed on the same database server ? Does anybody know how to join those tables reasonable way ?
Thnxselect *
from db1.dbo.table1 t1 inner join db2.dbo.table2 t2
on t1.pk = t2.pk|||Check BOL (http://msdn2.microsoft.com/en-us/library/ms187879.aspx).

-PatP|||Use Linked Servers concept and BOL is your friend.|||Linked servers unnecessary since both dbs on same server.

Saturday, February 25, 2012

run a .sql in different databases

I want to know if it is posible to run one query file in different databases

right now I have 2 different .sql and 6 databases and I want to run it in the following order

queries1.sql on db1
quieries2.sql on db2
quieries1.sql on db3
queries2.sql on db4
quieries1.sql on db5
queries2.sql on db6

all this just in one execution and they cannot run at the same time

Check the utility SQLCMD.EXE in BOL, or OSQL.EXE if you are using SS 2000.

AMB

|||im using MSSQL server 2005
|||

Relatively simple using a SQLCmd.exe script.

Refer to Books Online for the complete syntax, description, and samples.

|||i found this

Code Snippet

Create a folder named MyFolder. Use the File/Save As menu to save the above script as a file named MyScript.sql in the folder C:\MyFolder. Run the following command from the command prompt to run the script and place the output in a file named MyOutput.txt in the same folder:


sqlcmd -i C:\MyFolder\MyScript.sql -o C:\MyFolder\MyOutput.txt

When you view the contents of MyOutput.txt in Notepad, you will see the following:


Changed database context to 'AdventureWorks'.
ContactID FirstName LastName
-- -- --
1 Syed Abbas
2 Catherine Abel
3 Kim Abercrombie
4 Humberto Acevedo
5 Pilar Ackerman

(5 rows affected)


but how can I say to it, run myscript.sql in the database db1 for example
|||

Check BOL for more info about SQLCMD.EXE parameters.

-d use database name

AMB

|||you mean something like this?

sqlcmd -i C:\MyFolder\MyScript.sql -d db1 -o C:\MyFolder\MyOutput.txt
|||

Something I've done to execute a script against multiple databases is a DOS call.

Command Line Reference

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_o.mspx?mfr=true

for /F ["usebackqParsingKeywords"] {%% | %}variable in (`command`) do command [CommandLineOptions]

Here we go

for /F %a in ('select [name] from sysdatabases') do isql /E /i sql_script /d %a

The sqlcmd call is not much different than isql. I've also used this method to execute a script against multiple database servers. It can get a bit ugly, but it does the trick.

Good luck,

Bruce.

run a .sql in different databases

I want to know if it is posible to run one query file in different databases

right now I have 2 different .sql and 6 databases and I want to run it in the following order

queries1.sql on db1
quieries2.sql on db2
quieries1.sql on db3
queries2.sql on db4
quieries1.sql on db5
queries2.sql on db6

all this just in one execution and they cannot run at the same time

Check the utility SQLCMD.EXE in BOL, or OSQL.EXE if you are using SS 2000.

AMB

|||im using MSSQL server 2005
|||

Relatively simple using a SQLCmd.exe script.

Refer to Books Online for the complete syntax, description, and samples.

|||i found this

Code Snippet

Create a folder named MyFolder. Use the File/Save As menu to save the above script as a file named MyScript.sql in the folder C:\MyFolder. Run the following command from the command prompt to run the script and place the output in a file named MyOutput.txt in the same folder:


sqlcmd -i C:\MyFolder\MyScript.sql -o C:\MyFolder\MyOutput.txt

When you view the contents of MyOutput.txt in Notepad, you will see the following:


Changed database context to 'AdventureWorks'.
ContactID FirstName LastName
-- -- --
1 Syed Abbas
2 Catherine Abel
3 Kim Abercrombie
4 Humberto Acevedo
5 Pilar Ackerman

(5 rows affected)


but how can I say to it, run myscript.sql in the database db1 for example
|||

Check BOL for more info about SQLCMD.EXE parameters.

-d use database name

AMB

|||you mean something like this?

sqlcmd -i C:\MyFolder\MyScript.sql -d db1 -o C:\MyFolder\MyOutput.txt
|||

Something I've done to execute a script against multiple databases is a DOS call.

Command Line Reference

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_o.mspx?mfr=true

for /F ["usebackqParsingKeywords"] {%% | %}variable in (`command`) do command [CommandLineOptions]

Here we go

for /F %a in ('select [name] from sysdatabases') do isql /E /i sql_script /d %a

The sqlcmd call is not much different than isql. I've also used this method to execute a script against multiple database servers. It can get a bit ugly, but it does the trick.

Good luck,

Bruce.