Showing posts with label purposes. Show all posts
Showing posts with label purposes. Show all posts

Tuesday, March 27, 2012

Create table not working...?

I'm trying to create a couple of SQL commands that create and then destory a
database (for unit testing purposes).
I am currently just checking this all out (as my SQL isn't that great) in
Query Analyzer to make sure it works properly and have come across an issue.
This is my code--
BEGIN
CREATE DATABASE CynoNUnitTestDB
CREATE TABLE CynoUser
(
UserID int IDENTITY PRIMARY KEY,
UserName nvarchar(50)
)
END
Pretty simple stuff. However I then go into Enterprise manager or navigate
using the treeview on the left of Query Analyzer to find that my table
"CynoUser" has not been created! The database is there but the table isn't.
However if I then try to create it again it tells me the object already
exists!?!?! If so, then why can't I see it?
I'm assuming i've just got permissions wrong or something, can anyone tell
me what i'm doing wrong here?
Kind Regards
Jax"Simon Tamman {Uchiha Jax}"
< i_am_GETRIDOFTHISJUNKanti_everything@.NOS
PAMhotmail.com> wrote in message
news:5WA1f.18687$DO.13442@.newsfe3-gui.ntli.net...
> I'm trying to create a couple of SQL commands that create and then destory
> a
> database (for unit testing purposes).
> I am currently just checking this all out (as my SQL isn't that great) in
> Query Analyzer to make sure it works properly and have come across an
> issue.
> This is my code--
> BEGIN
> CREATE DATABASE CynoNUnitTestDB
> CREATE TABLE CynoUser
> (
> UserID int IDENTITY PRIMARY KEY,
> UserName nvarchar(50)
> )
> END
>
You created CynoUser in another database, probably Master.
After you create a database, you need to connect to it and then create the
table.
CREATE DATABASE CynoNUnitTestDB
GO
USE CynoNUnitTestDB
GO
CREATE TABLE CynoUser
(
UserID int IDENTITY PRIMARY KEY,
UserName nvarchar(50)
)
This script issues three seperate batches (seperated by 'GO'). The first
batch creates the database, the second switches your connection's current
database context, the third creates the table in the new database context.
David|||You've created the database, so what? The table CynoUser will be created in
the database you're in. How is SQL Server supposed to know that you want
the table to created in some other database than your current context?
Try:
CREATE DATABASE CynoNUnitTestDB
GO
USE CynoNUnitTestDB
GO
CREATE TABLE dbo.CynoUser ...
--or
CREATE DATABASE CynoNUnitTestDB
GO
CREATE TABLE CynoNUnitTestDB.dbo.CynoUser ...
"Simon Tamman {Uchiha Jax}"
< i_am_GETRIDOFTHISJUNKanti_everything@.NOS
PAMhotmail.com> wrote in message
news:5WA1f.18687$DO.13442@.newsfe3-gui.ntli.net...
> I'm trying to create a couple of SQL commands that create and then destory
> a
> database (for unit testing purposes).
> I am currently just checking this all out (as my SQL isn't that great) in
> Query Analyzer to make sure it works properly and have come across an
> issue.
> This is my code--
> BEGIN
> CREATE DATABASE CynoNUnitTestDB
> CREATE TABLE CynoUser
> (
> UserID int IDENTITY PRIMARY KEY,
> UserName nvarchar(50)
> )
> END
> --
> Pretty simple stuff. However I then go into Enterprise manager or
> navigate
> using the treeview on the left of Query Analyzer to find that my table
> "CynoUser" has not been created! The database is there but the table
> isn't.
> However if I then try to create it again it tells me the object already
> exists!?!?! If so, then why can't I see it?
> I'm assuming i've just got permissions wrong or something, can anyone tell
> me what i'm doing wrong here?
> Kind Regards
> Jax
>
>|||Thank you very much David and Aaron.
Works perfectly now.
CREATE DATABASE CynoNUnitTestDB
GO
USE CynoNUnitTestDB
GO
CREATE TABLE CynoUser
(
UserID int IDENTITY PRIMARY KEY,
UserName nvarchar(50)
)
Is all good. :)
You'll have to excuse me Aaron, i'm not used to creating databases and
tables in SQL so the whole issue of scope completely passed me by. Hopefully
i'll get better soon :).
"Simon Tamman {Uchiha Jax}"
< i_am_GETRIDOFTHISJUNKanti_everything@.NOS
PAMhotmail.com> wrote in message
news:5WA1f.18687$DO.13442@.newsfe3-gui.ntli.net...
> I'm trying to create a couple of SQL commands that create and then destory
a
> database (for unit testing purposes).
> I am currently just checking this all out (as my SQL isn't that great) in
> Query Analyzer to make sure it works properly and have come across an
issue.
> This is my code--
> BEGIN
> CREATE DATABASE CynoNUnitTestDB
> CREATE TABLE CynoUser
> (
> UserID int IDENTITY PRIMARY KEY,
> UserName nvarchar(50)
> )
> END
> --
> Pretty simple stuff. However I then go into Enterprise manager or
navigate
> using the treeview on the left of Query Analyzer to find that my table
> "CynoUser" has not been created! The database is there but the table
isn't.
> However if I then try to create it again it tells me the object already
> exists!?!?! If so, then why can't I see it?
> I'm assuming i've just got permissions wrong or something, can anyone tell
> me what i'm doing wrong here?
> Kind Regards
> Jax
>
>|||Thank you very much for your help.
All works fine now!
Thankee.
"Simon Tamman {Uchiha Jax}"
< i_am_GETRIDOFTHISJUNKanti_everything@.NOS
PAMhotmail.com> wrote in message
news:5WA1f.18687$DO.13442@.newsfe3-gui.ntli.net...
> I'm trying to create a couple of SQL commands that create and then destory
a
> database (for unit testing purposes).
> I am currently just checking this all out (as my SQL isn't that great) in
> Query Analyzer to make sure it works properly and have come across an
issue.
> This is my code--
> BEGIN
> CREATE DATABASE CynoNUnitTestDB
> CREATE TABLE CynoUser
> (
> UserID int IDENTITY PRIMARY KEY,
> UserName nvarchar(50)
> )
> END
> --
> Pretty simple stuff. However I then go into Enterprise manager or
navigate
> using the treeview on the left of Query Analyzer to find that my table
> "CynoUser" has not been created! The database is there but the table
isn't.
> However if I then try to create it again it tells me the object already
> exists!?!?! If so, then why can't I see it?
> I'm assuming i've just got permissions wrong or something, can anyone tell
> me what i'm doing wrong here?
> Kind Regards
> Jax
>
>

Tuesday, February 14, 2012

Create copy of database and place on different server

All,
I am trying to create a copy of an existing database and place on a different sql server for testing purposes. Currently my method of doing this is to create a backup, then restore a database on my new sql server. But here is the problem I am running into. When I look in Enterprise manager I can see all of the stored procedures and tables and data just fine, which is how I would like it. But, when I open up Query Analyzer I am not able to run any queries because it says "invalid object name" error. I know that the object name is correct. I think that I am having a conflict between the users which were carried over from the source backup file and the users that are on my 2 new sql server. If anyone could help, I would really appreciate it. I am stuck at this point. I am guessing it is probably something simple I am just unaware that I need to do.

Thanks,
LinsLoHi,

Maybe you should try a detach

EXEC sp_detach_db 'your_db'

And then on the second server an attach:

EXEC sp_attach_db @.dbname = N'pubs',
@.filename1 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs.mdf',
@.filename2 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs_log.ldf'|||LinsLo,

The problem is probably being caused because the userids are not matching up in your system tables.

First make sure the usernames for the database exist on the server then for each user run the following sql script.

EXEC sp_change_users_login 'Update_One', '<USERNAME>', '<USERNAME>'

That will align the users in the database with the users on the server.

Hope this helps.|||My first guess would be that you need to use sp_change_users_login (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_sp_ca-cz_8qzy.asp).

-PatP