Change Database In Procedure

Apr 4, 2008

I work at a medical billing office. We provide billing services for a number of clients. The primary billing software runs on a big AIX system, but for reporting and a few other things we get a daily dump into sql server. The way the dump happens each client get's their own database in sql server. This works great for reports that go to individual clients, but it can be a real pain for internal reports that cover all clients. We end up with sql code that looks something like this:
SQL Code:






Original
- SQL Code




ALTER PROCEDURE ProceName
AS
BEGIN
CREATE TABLE #Temp
(
Client varchar(5),
FieldName varchar(20),
OtherField int
)

DECLARE @Client varchar(5)
DECLARE @SQL varchar(8000)
DECLARE curAll cursor FOR /*SQL Code to get Client list here */
OPEN curAll
FETCH NEXT FROM curAll INTO @Client
WHILE @@FETCH_STATUS = 0
BEGIN
Set @SQL =
'SELECT ''' + @Client + ''', *'
+ ' FROM dump_' + @Client + '.dbo.TableName t'
+ ' WHERE t.FieldName=''somevalue'''

INSERT INTO #Temp
Exec(@SQL)

FETCH NEXT FROM curAll INTO @Client
END

--send results to application
SELECT * FROM #Temp

--clean up
CLOSE curAll
DEALLOCATE curAll
DROP Table #Temp
END






ALTER PROCEDURE ProceNameASBEGIN    CREATE TABLE #Temp    (       Client varchar(5),       FieldName varchar(20),       OtherField int    )     DECLARE @Client varchar(5)       DECLARE @SQL varchar(8000)    DECLARE curAll CURSOR FOR /*SQL Code to get Client list here */    OPEN curAll    FETCH NEXT FROM curAll INTO @Client    WHILE @@FETCH_STATUS = 0    BEGIN          SET @SQL =             'SELECT ''' + @Client + ''', *'        + ' FROM dump_' + @Client + '.dbo.TableName t'        + ' WHERE t.FieldName=''somevalue'''         INSERT INTO #Temp            Exec(@SQL)          FETCH NEXT FROM curAll INTO @Client    END      --send results to application    SELECT * FROM #Temp     --clean up    CLOSE curAll    DEALLOCATE curAll    DROP TABLE #TempEND


As you can imagine, this is a real pain. There must be someway to dynamically change the current database in the procedure and run the code there directly rather than creating a string and calling out to exec 50+ times. Any ideas?

View 5 Replies


ADVERTISEMENT

The Old Inability To Toggle/change/switch Between ALTER PROCEDURE &<---&> CREATE PROCEDURE Bug (or Is It A Feature?)

Apr 1, 2007

Keep in mind this is my first compiled SQL program Stored Procedure(SP), copied from a book by Frasier Visual C++.NET in Visual Studio2005 (Chap12). So far, so theory, except for one bug (feature?)below. At some point I'm sure I'll be able to laugh about this, akinto forgeting a semi-colon in C/C++, but right now it's frustrating(time to sleep on it for a while).Problem--For some reason I get the error when trying to save files where twotables (called Author and Content), linked by a single key, form arelationship.By simple comparison of the source code in the textbook and my program(below) I found the difference: instead of, like in the textbook, theStored Procedure (SP) starting with "CREATE PROCEDURE", it*automatically* is (was somehow) given the name of 'ALTER PROCEDURE'and I cannot change this to "CREATE PROCEDURE" (you get an error in MSVisual Studio 2005 Pro edition of "There is already an object namedXXX in the database", see *|* below). No matter what I do, the SP isalways changed by Visual Studio 2005 to 'ALTER PROCEDURE'!!!(otherwise it simply will not save)Anybody else have this happen? (See below, others have had this happenover the years but it's not clear what the workaround is)Keep in mind this is my first attempt and I have ordered somespecialized books on SQL, but if this is a common problem (and Isuspect it's some sort of bug or quirk in VS2005), please let me know.Frankly I think SQL as done by VS2005 is messed up.Here are two Usenet threads on this problem:(1) http://tinyurl.com/2o956m or,http://groups.google.com/group/micr...1454182ae77d409(2) http://tinyurl.com/2ovybv or,http://groups.google.com/group/micr...9e5428bf0525889The second thread implies this is a bug--any fix?Also this bug might be relate to the fact I've switched (and notrebooted) from Administrator to PowerUser after successfully changingthe permissions in the SQL Server Management Studio Express (see thisthread: http://tinyurl.com/2o5yqa )Regarding this problem I might try again tommorrow to see if rebootinghelps.BTW, in the event I can't get this to work, what other SQL editor/compiler should I use besides MS Visual Studio 2005 for ADO.NET andSQL dB development?RL// source files// error message:'Authors' table saved successfully'Content' table- Unable to create relationship 'FK_Content_Authors'.The ALTER TABLE statement conflicted with the FOREIGN KEY constraint"FK_Content_Authors". The conflict occurred in database "DCV_DB",table "dbo.Authors", column 'AuthorID'.// due to the below no doubt!--CREATE PROCEDURE dbo.InsertAuthor /* THIS IS CORRECT (what I want)'CREATE PROCEDURE' not 'ALTER PROCEDURE'*/(@LastName NVARCHAR(32) = NULL,@FirstName NVARCHAR(32) = NULL)AS/* SET NOCOUNT ON */INSERT INTO Authors (LastName, FirstName)VALUES(@LastName, @FirstName)RETURN--ALTER PROCEDURE dbo.InsertAuthor /* WRONG! I want 'CREATE PROCEDURE'not 'ALTER PROCEDURE' but VS2005 won't save it as such!!!*/(@LastName NVARCHAR(32) = NULL,@FirstName NVARCHAR(32) = NULL)AS/* SET NOCOUNT ON */INSERT INTO Authors (LastName, FirstName)VALUES(@LastName, @FirstName)RETURN--*|* Error message given: when trying to save CREATE PROCEDURE StoredProcedure: "There is already an object named 'InsertAuthor' in the dB

View 11 Replies View Related

How To Change The Data Format For A Single Database In A SQL Server Group Of Database

Nov 1, 2001

Hi all,

I have to change the date format for one database in a group of databases in my sql server 2000,Can you please tell me how to change the date format.


thanks in advance

View 1 Replies View Related

How To Use Stored Procedure To Change Permission??

Apr 24, 2001

Hi,
I have a schedule task which call one of my stored procedure,
In this stored procedure, I need to change db owner of one of
database, but I find sp_changedbowner do not allow me to specify
db name,it only change current db,so I have to open a db before
call sp_changedbowner,but it is invalid..


CREATE PROC demo
as
begin
...

use demo_db //it is invalid
exec sp_changedbowner 'scott'

end

Can anyone give me ideas?

Thanks

View 1 Replies View Related

I Need To Change The Owner Of A Stored Procedure To Dbo! Help!

May 18, 2001

Hello. I am trying to change the owner of a stored procedure from a user ID to dbo. How do i do this. I have tried sp_changeobjectowner but I get an error message. Can I do this from Enterprise Manager? btw, I am using SQL 7.0. Any help would be appreciated. Thanx

View 2 Replies View Related

Stored Procedure To Change Default

Dec 14, 2005

Can someone explain to me why the following doesn't work?declare @oname sysnameselect @oname=name from sysobjects where name like"df__mytable__mycol%"alter table mytable drop constraint @oname

View 4 Replies View Related

Change In Database User Perms When Copying Database

Mar 9, 2000

How to maintain Database User Permissions when copying the Database from One SQL Server to another(Either through backups or sp_detach). The reason is the login sid is different in the target server and as a result the database user is not able to map to the login existing in the target server. The only way I can correct this is through dropping and recreating the user's again and assign the permissions, or change the system catalog - sysusers to remap the login to the user in the database.
I do not wish to use the sp_addalias as it is available only for backward compatibility.
Is there a better way of doing this ?

View 4 Replies View Related

System Stored Procedure Name That Allows One To Change One's Password ?

Jan 2, 2008

I am using a web application using asp.net 1.1 vs 2003.
i am using sql server authentication where users are using their sql server user id and sql server saved passwords.
how can i change their passwords inside the sql server?
is there a system sp that allows one to pass one's user id and then change password.
 I will need to call that sp from a user defined store dprocedure and pass the parameters and that will change the password on the sql server.
thanks
  
 

View 3 Replies View Related

How To Change Date Formats In Stored Procedure

Oct 4, 2005

I need help on how to change the date format in a stored procedure. I am using the GetDate() function but need to convert it to short date format.

thanks
mike

View 14 Replies View Related

Change Data Format In Stored Procedure

Jul 31, 2006

Hi All,

My scenario is that I want to change the default SQL server format in my stored procedure more preferably only during the course of stored procedure execution (not permanent changes does any one have idea that how will I able to achieve this simple task...

regards,

Anas

View 1 Replies View Related

How To Create Stored Procedure Change Value Once A Month From 1,2,3,4 TO 2,3,4,1

May 8, 2008

help
i need to create stored procedures that run once month
change value (UPDATE) once a month in one table the values from 1,2,3,4 next month 2,3,4,1 ..... 3,4,2,1 .......4,1,2,3 ..... 1,2,3,4
i have this code



Code Snippet
SELECT empID, location, ISNULL(NULLIF (( location + DATEDIFF(mm, location_date, GETDATE())) % 4, 0), 4) AS new_location
FROM dbo.empList

like this the employee go from one location once amonth to new location






table on this month (5)
empid location location_date
--------------------------------------------------------------

1111 1
222 2
333 3
444 4

5555 3
666 2
777 3
888 4

table on next month (6) (after a month)
empid location location_date
-------------------------------------------------------------------

1111 2
222 3
333 4
444 1

5555 4
666 3
777 4
888 1


TNX

View 16 Replies View Related

How Do I Change The Type Of A Stored Procedure From User To System?

Aug 11, 2005

I built a database by using a generated script from the originaldatabase. It built the System Store Procedures as User type. How do Ichange them back to System type?

View 4 Replies View Related

Proper Procedure To Change Servername Of Replicated Environemnts?

Jul 22, 2005

Hi There

View 4 Replies View Related

DB Engine :: Stored Procedure Change History Information?

Jun 5, 2015

one of my SQL Developer member had one observation that, size of the parameter 'Parameter_XYZ' in certain stored procedure had changed from 25 to 255 during some production fixes, however suddenly its looks like that, someone has changed it back to 25 instead of 255.

DECLARE @Parameter_XYZ
varchar(25);

Can we figure out in which sprint/drop the stored procedure was changed and the Parameter_XYZ back to 25. Can any log recovery mechanism will get such details.

Can we get stored procedure text between different alteration.

View 4 Replies View Related

How To Change View Code Or Table Form Stored Procedure + -

Jun 1, 2008

need help with my problem
i have this view
this code change the value field "new_unit" evry month from 1 > 2 > 3 > 4
like this evry 4 month it return to 1 >2..........
------------------------------------------ for example

if i put
unit_date = 01/05/2008
and unit=1
than new_unit=1




Code Snippet

SELECT empID, ShiftType, unit_date, unit, ISNULL(NULLIF ((unit - DATEDIFF(mm, unit_date, '01/01/' + CONVERT(varchar, YEAR(GETDATE())))) % 4, 0), 4)

AS new_unit

FROM dbo.empList




my question is how to create a stored procedure that move forward (all the employee) the "new_unit" field
in +1 OR "unit_date" value MONTH +1


like create stored procedure name "plus" + so if i run this name stored procedure name "plus"

the stored procedure go to the viewor table and change the code view or table value

so i forward all the the "new_unit" or "unit_date" value IN one (change the cycle)+1

it doesn't matter if it change the "unit" value in the table "dbo.empList" or "unit_date" value

the important thing is that i can forward +1 or backward -1

evry time i run the stored procedure i get +1 (in the "new_unit")
and olso
create stored procedure name "minus" + so if i run this name stored procedure name "minus"
this stored procedure that move backward the the "unit" value in the table "dbo.empList" or "unit_date" value in -1

TABLE dbo.empList

empid ShiftType unit_date unit

----------------------------------------------------------------------------
11111 6 01/01/2008 1
2222 8 01/03/2008 4
3333 9 01/04/2008 3

TNX for the help

View 7 Replies View Related

How Can I Change The Data Type Of The Parameter For The Deployed Stored Procedure ??

Jan 11, 2006

Hi
 
I have Try to Create Stored Procedure in C# with the following structure
 
[Microsoft.SqlServer.Server.SqlProcedure]
public static void sp_AddImage(Guid ImageID, string ImageFileName, byte[] Image)
{
       
}
 
But  when I try to deploy that SP to SQL Server Express  , The SP Parameters become in the following Stature
 
@ImageID uniqueidentifier
@ImageFileName nvarchar(4000)
@Image varbinary(8000)
 
But I don€™t want that Data types .. I want it to be in the following format
 
@ImageID uniqueidentifier
@ImageFileName nText
@Image Image
 
 
How Can I Control the data type for each parameter ??
Or
How Can I Change the data type of the parameter for the Deployed Stored Procedure ??
Or
How Can I defined the new Data type ??
 
Or
 
What's the solution to this problem ??
 
Note : I get Error when I try to use Alert Statement to change the parameter Data type for the SP
 
ALTER PROCEDURE [dbo].[sp_AddImage]
      @ImageID [uniqueidentifier],
      @ImageFileName nText,
      @Image Image
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [DatabaseAndImages].[StoredProcedures].[sp_AddImage]
GO
 
And thanks with my best regarding
Fraas

View 7 Replies View Related

NORTHWIND Database Was Re-created From A Different Database:How Can I Change The Entry In Sysdatabases For Database 'NORTHWIND'?

Jan 14, 2008

Hi all,

From the http://msdn.microsoft.com/en-us/library/bb384469.aspx (Walkthrough: Creating Stored Procedures for the Northwind Customers Table, I copied the following sql code:

--UpdateSPforNWcustomersTable.sql--

USE NORTHWIND

GO

IF EXISTS (SELECT * FROM sysobjects WHERE name = 'SelectCustomers' AND user_name(uid) = 'dbo')

DROP PROCEDURE dbo.[SelectCustomers]

GO

CREATE PROCEDURE dbo.[SelectCustomers]

AS

SET NOCOUNT ON;

SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM dbo.Customers

GO

IF EXISTS (SELECT * FROM sysobjects WHERE name = 'InsertCustomers' AND user_name(uid) = 'dbo')

DROP PROCEDURE dbo.InsertCustomers

GO

CREATE PROCEDURE dbo.InsertCustomers

(

@CustomerID nchar(5),

@CompanyName nvarchar(40),

@ContactName nvarchar(30),

@ContactTitle nvarchar(30),

@Address nvarchar(60),

@City nvarchar(15),

@Region nvarchar(15),

@PostalCode nvarchar(10),

@Country nvarchar(15),

@Phone nvarchar(24),

@Fax nvarchar(24)

)

AS

SET NOCOUNT OFF;

INSERT INTO [dbo].[Customers] ([CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax]) VALUES (@CustomerID, @CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax);

SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers WHERE (CustomerID = @CustomerID)

GO

IF EXISTS (SELECT * FROM sysobjects WHERE name = 'UpdateCustomers' AND user_name(uid) = 'dbo')

DROP PROCEDURE dbo.UpdateCustomers

GO

CREATE PROCEDURE dbo.UpdateCustomers

(

@CustomerID nchar(5),

@CompanyName nvarchar(40),

@ContactName nvarchar(30),

@ContactTitle nvarchar(30),

@Address nvarchar(60),

@City nvarchar(15),

@Region nvarchar(15),

@PostalCode nvarchar(10),

@Country nvarchar(15),

@Phone nvarchar(24),

@Fax nvarchar(24),

@Original_CustomerID nchar(5)

)

AS

SET NOCOUNT OFF;

UPDATE [dbo].[Customers] SET [CustomerID] = @CustomerID, [CompanyName] = @CompanyName, [ContactName] = @ContactName, [ContactTitle] = @ContactTitle, [Address] = @Address, [City] = @City, [Region] = @Region, [PostalCode] = @PostalCode, [Country] = @Country, [Phone] = @Phone, [Fax] = @Fax WHERE (([CustomerID] = @Original_CustomerID));

SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers WHERE (CustomerID = @CustomerID)

GO

====================================================================================
I executed the above code in my SQL Server Management Studio Express (SSMSE) and I got the following error messages:

Msg 911, Level 16, State 1, Line 1

Could not locate entry in sysdatabases for database 'NORTHWIND'. No entry found with that name.

Make sure that the name is entered correctly.

===============================================================================================================
I know I recreated the NORTHWIND Database from a different Database before and I did not do anything for the entry in sysdatabases. How can I change the entry in sysdatabases for database 'NORTHWIND' now? Please help and advise.

Thanks in advance,
Scott Chang

View 5 Replies View Related

Error Message Given Using Access 2003 Adp File To Change A Stored Procedure

Sep 17, 2007

Client/Server machine: Windows Xp Pro (SP2) (latest patches)
Office Software: Access 2003 (latest patches)
Database S/W: SQL Server 2005 (latest patches)

The following error message is displayed when trying to modify a stored procedure.

This version of Microsoft Access doesn't support design changes to the
version of Microsoft SQL Server your project is connected to. See the
Microsoft Office Update Web site for the latest information and downloads
(on the Help menu, click Office on the Web). Your design changes will not be
saved.

However, if you save, close and re-open the stored procedure having made the required changes, the changes have been saved.

Is there any way to suppress the error message / hotfix available from microsoft since the error message appears to be completely erroneous ?

Have I provided enough detail as this is my first post ?


Philip


View 1 Replies View Related

Change Database Name

Jul 21, 2004

Hi, i have database in sql server called search, but today, i found it is changed to search (Single user), I don't know who did it. How can I change to mutilple users, also how to change the name back to search. Thanks.

View 10 Replies View Related

***How Do I Change A Database Name?***

Dec 31, 2006

Hi,

This is my first posting to this forum. I'm trying to
I'm trying to change the name of my database from ASPNET.mdf to CulturedStar.mdf. I need to change the physical name and logical name.

I'm working with Microsoft SQL Server Management Studio Express.

How do I do this?

Thank you so much for your time and effort ~ Paul

View 8 Replies View Related

Change The In Use Database

Mar 26, 2007

In SMSS or QA why the following code does not change the database in use to My_db? The value for @DynSQL is USE [My_db].

USE [master]
GO

DECLARE @db_name varchar(100), @DynSQL varchar(512)

SET @db_name = 'My_db'

SET @DynSQL = 'USE [' + @db_name + ']'

Print @DynSQL
EXEC (@DynSQL)
GO


It seems the change occurs inside the dynamic portion because if I SELECT a table within that database, it works. For example the following works but it does not change the default database to My_db.

SET @DynSQL = 'USE [' + @db_name + ']'+' SELECT * FROM Mytable'

View 3 Replies View Related

How To Get The Ddl Of Every Change In The Database

Mar 15, 2008

How to get the ddl of every change in the database
Includes changes in the table, column, constraints's
That is a new table added, table drop table name changed, column type change column added, primary keys , foreign key, check constraints e.t.c

I am not looking for the changes in triggers. stored procedures, functions.

Kamran Shahid
Sr. Software Engineer(MCSD.Net)
www.netprosys.com

View 8 Replies View Related

Change Database Language

Dec 30, 2006

i using sql server Express, i want to change the default language from US-English to British EnglishWhere and How?
 

View 3 Replies View Related

Database Owner Change

Jun 27, 2007

 Hello,I'm using tableadapters in VWD 2005 Express to link our ASP.NET application to a SQL Server 2000 database.  Initially, I used Database Explorer to drop tables into the DataSet object, which picks up the owner of the tables from the database.Recently, we had to change the owner of all objects in the database, including tables and stored procedures.  When we run our application it chokes on stored procedures because the owner of the SP's has changed.  My question is: how can I update tables and tableadapters in the DataSet to reflect the change in owner, without having to recreate everything in DataSet?  

View 1 Replies View Related

How To Change Owner Of Database? Please Help!

Jul 29, 2007

 Hi,I just transferred my website and database (SQL 2000) to a new host who's SQL Manager doesn't support the previous username I had for the database.The previous owner of database tables etc was Database_master and now I want to change it to just Master.Please advise! Someone else designed the website for me, and honestly I have just some basic knowledge of databases and such.I would appreciate help and if possible with detailed steps.Many thanks in advance,Isje 

View 2 Replies View Related

Change Database Password

Sep 8, 2007

I have a SQL database hosted on a server. This server supports SQL Sever 2000. How do I change the password to the password?Thanks in advance, 

View 1 Replies View Related

Change Database Time

Feb 25, 2008

Hello, is there a way to set the database to my local time?
I am having troubles using the Membership.GetNumberofUsersOnline method - it is showing users online who in test mode I have logged off, but I think due to time zone difference between my server and myself, still appear logged on.
This is causing further difficulties in using my admin functions to edit user information, because for some reason  I cannot edit a user who is logged on...
Help anyone?

View 1 Replies View Related

How Do I Change The Owner Of My SQL Database?

May 17, 2008

Hello,
 I'm trying to access the Database Diagram of a sql database I created but am getting the error:
 
"The database does not have a valid dbo user"
 I also noticed that my NT group user was incorrect and this is the name being used as the owner of the database. How do i change this?
 
Thanks

View 3 Replies View Related

Change The Owner Of The Database

Jan 10, 2001

Hi All,

I want to change the OWNER of the database to 'sa' and I executed the
following system stored procedure

EXEC sp_changedbowner 'sa'

and I get the following error message..

Server: Msg 15110, Level 16, State 1, Procedure sp_changedbowner, Line 46
The proposed new database owner is already a user in the database.

Any ideas
Thanks
Reddy

View 1 Replies View Related

How To Change The Database Owner In SQL 7

Oct 18, 2000

Is there a stored procedure to change the database owner in SQL 7?

Thanks in advance,
Faustina

View 4 Replies View Related

Change The Owner Of Database

May 26, 2004

Dear Ladies & Gentlemen,

I need help here. I have problem on my Database own by 'unknown". Can I change the onwner of that Database.

Detail of SQL:
Micosoft SQL Server 2000

If anybody can help me, please reply this thread.

Regards

View 1 Replies View Related

How To Change COLLATION NAME For The Database?

Feb 12, 2004

Is there a way (besides "ALTER DATABASE COLLATE ...") to change collation name for the whole database? I tried to use the "ALTER DATABASE" command, but it didn't work. And I wouldn't like to run "ALTER COLUMN" commands for over 100 tables.

View 2 Replies View Related

Attach Database And Change It Name

May 4, 2007

Hi All.
I just start to use SQL2005 and don't know how attach .mdf file's database and change name of database at the same time?
Thanks.

View 5 Replies View Related







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