Hi all,
I got an error message 156, when I executed the following code:
////--SQLQueryParent&Child.sql---////////
Use
newDBGO
----Creating dbo.Person as a Parent Table----
CREATE
TABLE dbo.Person (PersonID int PRIMARY KEY NOT NULL,FirstName
varchar(25) NOT NULL,LastName
varchar(25) NOT NULL,City
varchar(25) NOT NULL,State
varchar(25) NOT NULL,Phone
varchar(25) NOT NULL)INSERT
dbo.Person (PersonID, FirstName, LastName, City, State, Phone)SELECT
1, "George", "Washington", "Washington", "DC", "1-000-1234567"UNION
ALLSELECT
2, "Abe", "Lincoln", "Chicago", "IL", "1-111-2223333"UNION
ALLSELECT
3, "Thomas", "Jefferson", "Charlottesville", "VA", "1-222-4445555"GO
----Creating dbo.Book as a Child table----
CREATE
TABLE dbo.Book (BookID int PRIMARY KEY NOT NULL,BookTitle
varchar(25) NOT NULL,AuthorID
int FOREIGN KEY NOT NULL)INSERT
dbo.Book (BookID, BookTitle, AuthorID)SELECT
1, "How to Chop a Cherry Tree", 1UNION
ALLSELECT
2, "Valley Forge Snow Angels", 1UNION
ALLSELECT
3, "Marsha and ME", 1UNION
ALLSELECT
4, "Summer Job Surveying Viginia", 1UNION
ALLSELECT
5, "Log Chopping in Illinois", 2UNION
ALLSELECT
6, "Registry of Visitors to the White House", 2UNION
ALLSELECT
7, "My Favorite Inventions", 3UNION
ALLSELECT
8, "More Favorite Inventions", 3UNION
ALLSELECT
9, "Inventions for Which the World is Not Ready", 3UNION
ALLSELECT
10, "The Path to the White House", 2UNION
ALLSELECT
11, "Why I Do not Believe in Polls", 2UNION
ALLSELECT
12, "Doing the Right Thing is Hard", 2GO
---Try to obtain the LEFT OUTER JOIN Results for the Parent-Child Table
SELECT
* FROM Person AS I LEFT OUTER JOIN Book ON I.ID=P.IDGO
////---Results---//////
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'NOT'.
////////////////////////////////////////////////////
(1) Where did I do wrong and cause the Error Message 156
(2) I try to get a Parent-Child table by using the LEFT OUTER JOIN via the following code statement:
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'NOT'.
Can I get a Parent-Child table after the error 156 is resolved
Please help and advise.
Thanks,
Scott Chang

SSMS Express: Creating Parent-Child Table via LEFT OUTER JOIN - Error Message 156
Elizabeth Maher MSFT
Hi Umachandar, Thanks for your response.
I corrected my code as you instructed. I executed my revised code (see the listed code below) and I got the 2 tables "Person" and "Book" as well as a new error 4104:
Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "P.AuthorID" could not be bound.
I think I have a problem with the Foreign Key. Please help and advise how I can correct it.
Thanks again,
Scott Chang
/////////--revised---SQLqueryPerson&Book.sql---////////////////
Use
newDB--Creating dbo.Person as a Parent Table----
CREATE
TABLE dbo.Person (PersonID int PRIMARY KEY NOT NULL,FirstName
varchar(25) NOT NULL,LastName
varchar(25) NOT NULL,City
varchar(25) NOT NULL,State
varchar(25) NOT NULL,Phone
varchar(25) NOT NULL)INSERT
Person (PersonID, FirstName, LastName, City, State, Phone)SELECT
1, 'George', 'Washington', 'Washington', 'DC', '1-000-1234567'UNION
ALLSELECT
2, 'Abe', 'Lincoln', 'Chicago', 'IL', '1-111-2223333'UNION
ALLSELECT
3, 'Thomas', 'Jefferson', 'Charlottesville', 'VA', '1-222-4445555'GO
----Creating Book as a Child table----
CREATE
TABLE Book (BookID int PRIMARY KEY NOT NULL,BookTitle
varchar(50) NOT NULL,AuthorID
int NOT NULL FOREIGN KEY REFERENCES Person(PersonID) )INSERT
Book (BookID, BookTitle, AuthorID)SELECT
1, 'How to Chop a Cherry Tree', 1UNION
ALLSELECT
2, 'Valley Forge Snow Angels', 1UNION
ALLSELECT
3, 'Marsha and ME', 1UNION
ALLSELECT
4, 'Summer Job Surveying Viginia', 1UNION
ALLSELECT
5, 'Log Chopping in Illinois', 2UNION
ALLSELECT
6, 'Registry of Visitors to the White House', 2UNION
ALLSELECT
7, 'My Favorite Inventions', 3UNION
ALLSELECT
8, 'More Favorite Inventions', 3UNION
ALLSELECT
9, 'Inventions for Which the World is Not Ready', 3UNION
ALLSELECT
10, 'The Path to the White House', 2UNION
ALLSELECT
11, 'Why I Do not Believe in Polls', 2UNION
ALLSELECT
12, 'Doing the Right Thing is Hard', 2GO
---Try to obtain the LEFT OUTER JOIN Results for the Parent-Child Table
SELECT
* FROM Person AS I LEFT OUTER JOIN Book ON I.PersonID=P.AuthorIDGO
///////////////////--Results--/////////////////////
(3 row(s) affected)
(12 row(s) affected)
Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "P.AuthorID" could not be bound.
BlokHead
Hi Mark, Thanks for your response.
I corrected everything as you instructed. I executed the revised code and I got the following results and error messages:
(3 row(s) affected)
Msg 8152, Level 16, State 14, Line 7
String or binary data would be truncated.
The statement has been terminated.
Msg 207, Level 16, State 1, Line 2
Invalid column name 'ID'.
Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "P.ID" could not be bound.
I have no ideas how to make changes on the revised code. Please help and advise again.
Thanks again,
Scott Chang
pmukherj
Vasco Marques
BradN
Change the CREATE TABLE statement for Book to
CREATE TABLE dbo.Book
(BookID int PRIMARY KEY NOT NULL,
BookTitle varchar(25) NOT NULL,
AuthorID int NOT NULL FOREIGN KEY REFERENCES Person(PersonID)
)
I would also suggest changing the double quotes to single quotes
throughout
Domino
USE newDB
SELECT * FROM Person AS P
LEFT OUTER JOIN Book AS b
ON P.PersonID=b.AuthorID
GO
Wrangler
In addition to the error messages 8152, 207 and 4104, I found the Table "Person" is completely filled with 3 rows of input data and the Table "Book" is only filled with the titles and no input data. I think the "Foreign Key...REFERENCES Person(PersonID)" is not valid. Please help, tell me where I made mistakes and advise me how to correct the problems.
Thanks, Scott Chang
Visualcpp
Hello Umachandar, Thanks for your response.
I have created 2 tables (Person and Book) sucessfully and saved them in the database newDB. I executed the following code:
///--ParentChild.sql--///
USE newDB
SELECT * FROM Person AS P LEFT OUTER JOIN Book AS b ON b.PersonID=P.AuthorID
GO
I got the following 2 errors:
///--Messages--///
Msg 207, Level 16, State 1, Line 2
Invalid column name 'PersonID'.
Msg 207, Level 16, State 1, Line 2
Invalid column name 'AuthorID'.
I looked up the Books Online for more details on syntax of SELECT statement and how to specify/use table aliases and I could not find the materils you mentioned (Sorry! I am a beginner in doing T-SQL, SQL Server Express and SQL Server Management Studio Express). Please help and advise me how to resolve this problem.
Thanks,
Scott Chang