Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Statement Help 1

Status
Not open for further replies.

jgroh9

Technical User
Jun 13, 2003
17
US
All,
I have written some SQL code to create a database then 2 tables and after that insert some records into the new tables in the new database.
Everything works if I execute each SQL statement by itself. However, when I put all the SQL statements in one file and try to execute statements in Query Analyzer I get the following error:
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'Job_Title'.


Here is the code I have:
CREATE DATABASE Test
GO

CREATE TABLE Test..Job_Title
( Job_Title_Code nvarchar(50) NOT NULL
CONSTRAINT PK_Job_Title_Code PRIMARY KEY NONCLUSTERED,
Job_Title nvarchar(50) NOT NULL,
Exempt_Non_Exempt_Status bit NOT NULL,
Minimum_Salary decimal(11,2),
Maximum_Salary decimal(11,2)
);

CREATE TABLE Test..Employees
(
Social_Security_Number nvarchar (12) NOT NULL
CONSTRAINT PK_SSN PRIMARY KEY NONCLUSTERED,
Last_Name nvarchar (50),
First_Name nvarchar (50),
Address varchar (50),
City nvarchar (50),
State nvarchar (15),
Zip_Code numeric(5, 0) NULL,
Telephone_Area_Code numeric(3, 0) NULL,
Telephone_Number nvarchar (8),
Email_Address nvarchar (50),
Job_title_code nvarchar (50) NOT NULL
CONSTRAINT FK_Job_Title_Code FOREIGN KEY (Job_title_code)
REFERENCES Job_Title(Job_title_code) ON DELETE CASCADE ON UPDATE CASCADE,
Hire_date datetime NULL,
Salary decimal(11,2) NULL
);

'This is where the process fails!!
INSERT INTO Job_Title VALUES ('1', 'Actor', '1', '500000', '10000000');

Thanks in advance for the help!
 
Add these before you do the INSERT : USE Test
======================================================
CREATE DATABASE Test
GO

CREATE TABLE Test..Job_Title
( Job_Title_Code nvarchar(50) NOT NULL
CONSTRAINT PK_Job_Title_Code PRIMARY KEY NONCLUSTERED,
Job_Title nvarchar(50) NOT NULL,
Exempt_Non_Exempt_Status bit NOT NULL,
Minimum_Salary decimal(11,2),
Maximum_Salary decimal(11,2)
)

CREATE TABLE Test..Employees
(
Social_Security_Number nvarchar (12) NOT NULL
CONSTRAINT PK_SSN PRIMARY KEY NONCLUSTERED,
Last_Name nvarchar (50),
First_Name nvarchar (50),
Address varchar (50),
City nvarchar (50),
State nvarchar (15),
Zip_Code numeric(5, 0) NULL,
Telephone_Area_Code numeric(3, 0) NULL,
Telephone_Number nvarchar (8),
Email_Address nvarchar (50),
Job_title_code nvarchar (50) NOT NULL
CONSTRAINT FK_Job_Title_Code FOREIGN KEY (Job_title_code)
REFERENCES Job_Title(Job_title_code) ON DELETE CASCADE ON UPDATE CASCADE,
Hire_date datetime NULL,
Salary decimal(11,2) NULL
)
GO
USE Test /*USE THE NEWLY CREATED DATABASE*/
GO
INSERT INTO Job_Title VALUES ('1', 'Actor', '1', '500000', '10000000')
==========================================================
This should work.
 
MastermindSQL thanks for the help. Worked perfectly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top