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
>
>

No comments:

Post a Comment