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'
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 ?
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')
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.
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.
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 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.
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?
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
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?
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?
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?
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.
I have many tables and in those i require to change some data. Sayfrom ARCA to ARCAEX. I am sure that the string is unique in the sensethere will be no ARCAABC. So what do i do change by not manuallyneeding to search in each table and the whole database and still canbe sure that the changes have taken place. Please helpRegards,Rajesh
Please forgive me if I'm in the wrong category, and I know this is a duplicate post...
How do you deploy changes to a database? Is there something you can run to compare the two versions and create a script to do this? I've heard about the DTS package (now DTSX I believe) but am not sure how to set one up. If anyone could point me to an article or something that might clear things up, it would be greatly appreciated. Thank you!
I restored and renamed a client database from a backup file to my laptop (both SQL Express 2005) and noticed that no owner had been assigned. I set the db-owner to SA. This database will replace a local version of the same database that was created on my laptop. (The customer has entered data...)
When I start the dataset configuration wizard in VS2005 all tables appear twice in the list. Once with the original name and once with the prefix dbo. For example: Customer and dbo.Customer.
The tables with the dbo. prefix are marked with a Red Cross in the checkbox. Hovering over them display the error message: "Element .. in the dataset references an object missing from the database. "
The tables without the dbo. prefix are not marked. When I mark these tables VS2005 indicates that the table will be renamed to Customer1. The same issue applies to all the views in the database.
Is there a way to solve this problem without having to recreate the entire dataset? I'd rather not do that cause I added some queries to the dataset and they will be lost and have to be recreated.
Hi, it has to do with my unfamiliarity with creating users and dbo privileges. I'm trying to grant access to an outside vendor to import their latest version of a database. I set them up with a user account that had db_owner privileges, however when they ran the dts process it copied all the data as new tables rather than overwriting the existing tables and the new tables were all associated with the user is created instead of dbo user. What do I need to do to facilitate this process?
Is it possible, using VB code, to retain a user on a particular page until the status of a certain field in a database is changed? The scenario I am working on is that a person makes a reservation and has to wait until that reservation has been accepted or rejected by the administration.
Dim dsResv As DataSet dsResv = objResv.DALgetResvStatus(resvId)
While (dsResv.Tables(0).Rows(0).Item("resvStatus") = "pending")
dsResv = objResv.DALgetResvStatus(resvId)
If dsResv.Tables(0).Rows(0).Item("resvStatus") = "denied" Then failure.Visible = True LinkButton1.Visible = True
ElseIf dsResv.Tables(0).Rows(0).Item("resvStatus") = "reserved" Then success.Visible = True LinkButton1.Visible = True End If
End While
This is how I tried to do it, but it doesnt seem to work. Any suggestions?
Hi: I'm using SQL SERVER 2005 EXPRESS My server name is SERVER1SQLEXPRESS When I create a new data base on my server it saves at C:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLData How Can I change this default path? Thanks!!
hi, I have a database whose mdb and ldb files are located in C drive. I need to change these files to D drive.When i go to the database properties and try to change the location it does not allow me.It has given me a message(The database physical file cannot be changed once it is created). I can drop the table and recreate it from a BackUp but is there a better way to do this.
I am in the process of cleaning up the security on one of our production databases. SA is already the owner of the database.
On this SQL server, there is a login that has access to this database that needs to be removed. When I uncheck this users access to this database I receive the following error message: "Error 15183. The user owns objects in the database and cannot be dropped".
How can i find out what objects this logins has ownership of and how can I change the ownership of these objects to SA?
I have been migrating Databases from a SQL Server 7.0 Instance to a 2000 Instance. Basically the method I use is as follows:
a)Create a new Database on the Destination Server (same name as the Source) When I create the new Database on my destination server, the compatibility mode is '80' and the source is always '70'
b) do a 'revlogin' on the Source Server and use the output from that query to recreate the logins on the destination server
c) make sure that the default DB for the newly creates logins are correct and change them if necessary
d) Backup the DB on the Source Server and Restore it to the Destination Server.
e) Check login permissions and fix any orphaned users - usually I don't find any that need to be fixed, but I always check.
I've read BOL on Changing the compatibility mode on the Database... but I'm still unsure when I would have to do this and why???????????????? SHOULD I be changing the compatiblity mode from 80 to 70 when migrating Databases from 7.0 to 2000???? Any advice on moving DBs in this manner would be appreciated.
I have been doing relational database forever(or a long time) and have been intruduces to a team that uses a highly normalized database(propietary) to manage workflow.
We are capturing data in an AUDIT Trail EAV format.(500 million rows)
It is my task to build this into a data warehouse for reporting and I need to have with my team a relational database discussion. The relational database knowledge on this team is DB2 based, IDMS, and other past evolutions.
The common processes used are recieve a flat file and process this file sequentially using C# or VB doing lookups of other databse tables and writing out another flat file to be converted in XML for load to the propritary system.
My goal is to attempt to introduce new design concepts to my team and these are some talking points that I have come up with for a lunch and learn session.
can anyone else add to this list I don't want to get into a deep discussion about 3rd NF, Star Schemas vs Snowflake, etc.. I want to keep is informational and light to eliceit discussion and relat it back to older technologies.
some of the topics we can discuss are: Why the data warehouse Real-time tables what needs to stay in prod What is going to happen to reporting database Interaction between database on the same cluster/server Interaction between databases on different servers (linked servers not allowed) Set processing as opposed to cursor processing. Table types EAV Type1 Type2 Fact Dimensions Code
There have been several threads about changing a database's collation but none have come up with an easy answer before. The suggestion before was to create an empty database with the correct collation and then copy the data across. However this is hard work as you have to populate tables in a specific order in order not to violate foreign keys etc. You can't just dts the whole data.
There follows scripts we have written to do the job. If people use them, please could you add to this thread whether they worked successfully or not.
Firstly we change the default collation, then change all the types in the database to match the new collation.
=================== --script to change database collation - James Agnini -- --Replace <DATABASE> with the database name --Replace <COLLATION> with the collation, eg SQL_Latin1_General_CP1_CI_AS -- --After running this script, run the script to rebuild all indexes
ALTER DATABASE <DATABASE> COLLATE <COLLATION>
exec sp_configure 'allow updates',1 go reconfigure with override go update syscolumns set collationid = (select top 1 collationid from systypes where systypes.xtype=syscolumns.xtype) where collationid <> (select top 1 collationid from systypes where systypes.xtype=syscolumns.xtype) go exec sp_configure 'allow updates',0 go reconfigure with override go ===================
As we have directly edited system tables, we need to run a script to rebuild all the indexes. Otherwise you will get strange results like comparing strings in different table not working. The indexes have to actually be dropped and recreated in separate statements. You can't use DBCC DBREINDEX or create index with the DROP_EXISTING option as they won't do anything(thanks to SQL Server "optimization"). This script loops through the tables and then loops through the indexes and unique constraints in separate sections. It gets the index information and drops and re-creates it. (The script could probably be tidied up with the duplicate code put into a stored procedure).
==================== --Script to rebuild all table indexes, Version 0.1, May 2004 - James Agnini -- --Database backups should be made before running any set of scripts that update databases. --All users should be out of the database before running this script
print 'Rebuilding indexes for all tables:' go
DECLARE @Table_Name varchar(128) declare @Index_Name varchar(128) declare @IndexId int declare @IndexKey int
DECLARE Table_Cursor CURSOR FOR select TABLE_NAME from INFORMATION_SCHEMA.tables where table_type != 'VIEW'
OPEN Table_Cursor FETCH NEXT FROM Table_Cursor INTO @Table_Name
--loop through tables WHILE @@FETCH_STATUS = 0
BEGIN print '' print @Table_Name
DECLARE Index_Cursor CURSOR FOR select indid, name from sysindexes where id = OBJECT_ID(@Table_Name) and indid > 0 and indid < 255 and (status & 64)=0 and not exists(Select top 1 NULL from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where TABLE_NAME = @Table_Name AND (CONSTRAINT_TYPE = 'PRIMARY KEY' or CONSTRAINT_TYPE = 'UNIQUE') and CONSTRAINT_NAME = name) order by indid
OPEN Index_Cursor FETCH NEXT FROM Index_Cursor INTO @IndexId, @Index_Name
--loop through indexes WHILE @@FETCH_STATUS = 0 begin
declare @SQL_String varchar(256) set @SQL_String = 'drop index ' set @SQL_String = @SQL_String + @Table_Name + '.' + @Index_Name
set @SQL_String = @SQL_String + 'index ' set @SQL_String = @SQL_String + @Index_Name set @SQL_String = @SQL_String + ' on ' set @SQL_String = @SQL_String + @Table_Name
set @SQL_String = @SQL_String + '('
--form column list SET @IndexKey = 1
-- Loop through index columns, INDEX_COL can be from 1 to 16. WHILE @IndexKey <= 16 and INDEX_COL(@Table_Name, @IndexId, @IndexKey) IS NOT NULL BEGIN
IF @IndexKey != 1 set @SQL_String = @SQL_String + ','
set @SQL_String = @SQL_String + index_col(@Table_Name, @IndexId, @IndexKey)
SET @IndexKey = @IndexKey + 1 END
set @SQL_String = @SQL_String + ')'
print @SQL_String EXEC (@SQL_String)
FETCH NEXT FROM Index_Cursor INTO @IndexId, @Index_Name end
CLOSE Index_Cursor DEALLOCATE Index_Cursor
--loop through unique constraints DECLARE Contraint_Cursor CURSOR FOR select indid, name from sysindexes where id = OBJECT_ID(@Table_Name) and indid > 0 and indid < 255 and (status & 64)=0 and exists(Select top 1 NULL from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where TABLE_NAME = @Table_Name AND CONSTRAINT_TYPE = 'UNIQUE' and CONSTRAINT_NAME = name) order by indid
OPEN Contraint_Cursor FETCH NEXT FROM Contraint_Cursor INTO @IndexId, @Index_Name
--loop through indexes WHILE @@FETCH_STATUS = 0 begin
set @SQL_String = 'alter table ' set @SQL_String = @SQL_String + @Table_Name set @SQL_String = @SQL_String + ' drop constraint ' set @SQL_String = @SQL_String + @Index_Name
set @SQL_String = @SQL_String + '; alter table ' set @SQL_String = @SQL_String + @Table_Name set @SQL_String = @SQL_String + ' WITH NOCHECK add constraint ' set @SQL_String = @SQL_String + @Index_Name set @SQL_String = @SQL_String + ' unique '