Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
davidmal
Starting Member
19 Posts |
Posted - 2007-02-17 : 12:14:51
|
| On a server that has 50+ databases, each one has a 'students' table. I have a script that searches for records with the same first and last name (possible duplicates assigned different PKs). I'm trying to create a global stored procedure in the master DB that accepts the database name as a paramenter.I tried...CREATE PROCEDURE PossibleDuplicateStudent@Database varchar(50)USE @Databaseselect t.*from students tinner join ( select fname, lname from students group by fname, lname having count(*) > 1) don t.fname = d.fnameand t.lname = d.lnameENDGOWill not allow the USE statement so I tried...set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[PossibleDuplicateStudent]@Database varchar(50)ASBEGINselect t.*from CONCAT(@Database,".dbo.students") tinner join ( select fname, lname from CONCAT(@Database,".dbo.students") group by fname, lname having count(*) > 1) don t.fname = d.fnameand t.lname = d.lnameENDThis gives me "Invalid object name 'CONCAT'."Do I need to cast the concat statement as a DB object somehow? If so how?Please help.Thanks |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-02-17 : 12:39:49
|
read this for explanation of why below code works:http://weblogs.sqlteam.com/mladenp/archive/2007/01/18/58287.aspxhere you go:create database t1gouse t1create table test1 (col1 varchar(20))insert into test1 select 'In t1 DB'gocreate database t2gouse t2create table test1 (col1 varchar(20))insert into test1 select 'In t2 DB'gouse mastergocreate proc sp_GetTest -- must be prefixed with sp_asselect * from test1goexec sys.sp_MS_marksystemobject sp_GetTestgouse t1exec sp_GetTestgouse t2exec sp_GetTestgouse masterdrop database t1drop database t2drop proc sp_GetTest Go with the flow & have fun! Else fight the flow blog thingie: http://weblogs.sqlteam.com/mladenp |
 |
|
|
davidmal
Starting Member
19 Posts |
Posted - 2007-02-17 : 12:54:02
|
| Thanks for the reply and I looked over the link. This still will not accomplish what I'm attempting to do. I want to be able to open a query window and just enter EXEC sp_PossibleDuplicateStudents DBName and have the procedure run the query in the DB specified in the parameter. |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-02-17 : 12:59:36
|
then maybe you don't understand how this works.by doing this you don't have to pass in the database name.by marking the sproc in master as system object you execute it in the current database's context.the only requirement is that you have the table with the same name and structure in all databases.so you have to do:use dbNameexec sp_YourSprocGo with the flow & have fun! Else fight the flow blog thingie: http://weblogs.sqlteam.com/mladenp |
 |
|
|
davidmal
Starting Member
19 Posts |
Posted - 2007-02-17 : 13:10:15
|
| Ok I get it nowThanks spirit1 |
 |
|
|
|
|
|