Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need Query Help re:Create "Name" field & Populate

Author  Topic 

Bren582
Starting Member

14 Posts

Posted - 2008-01-25 : 09:12:51
Guys,
New to SQL and I'm trying to create a new field, entitled “Name” and see all employee names from my "Employee" table using the following format:
LastName, FirstName, jobcode, emailaddress and hire date.

after much effort my code is as follows:

CREATE VIEW 'Name'
AS
SELECT LastName, FirstName, JobID, Email, Hiredate AS Relationship
FROM 'Name'
Create View 'Name' as Select LastName, FirstName, JobID, Email, Hiredate From EMPLOYEE
Group By LastName, FirstName, JobID, Email, Hiredate Order By LastName

But i'm still getting the following errors:

Msg 111, Level 15, State 1, Line 196
'CREATE VIEW' must be the first statement in a query batch.
Msg 102, Level 15, State 1, Line 199
Incorrect syntax near 'Name'.
Msg 111, Level 15, State 1, Line 200
'CREATE VIEW' must be the first statement in a query batch.

Any assistance is greatly appreciated

Bren582
Starting Member

14 Posts

Posted - 2008-01-25 : 09:15:13
Update,, I Realized I was duplicating my code.. Removed the duplicate and now I'm down to a single error. I would imagine there is a routine to prevent the first query error??
Thanks for the assistance..

Code:
CREATE VIEW Names
AS
SELECT LastName, FirstName, JobID, Email, Hiredate AS Relationship
FROM Names
Group By LastName, FirstName, JobID, Email, Hiredate Order By LastName

Error:
Msg 111, Level 15, State 1, Line 196
'CREATE VIEW' must be the first statement in a query batch
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-25 : 09:34:11
The error suggests you have some statements that preceeds the view definition.Remove it and rerun.
Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2008-01-25 : 11:16:43
After you sort that out, you are going to get an error regarding your ORDER BY clause. You can't ORDER BY in a view unless you also use a TOP or FOR XML clause.

Jay
to here knows when
Go to Top of Page

Bren582
Starting Member

14 Posts

Posted - 2008-01-25 : 12:05:54
Guys, I'm not sure what you mean by "The error suggests you have some statements that preceeds the view definition.Remove it and rerun". I try running this as my first select query and receive the same error.. Please explain?? Thanks..
Go to Top of Page

Sara Karasik
Starting Member

10 Posts

Posted - 2008-01-25 : 12:56:10
Try to cut and paste it into a new Query window.
Go to Top of Page

Bren582
Starting Member

14 Posts

Posted - 2008-01-25 : 14:34:08
Its runs OK in a fresh query window, I am also able to run in my program the following code error free on the main query but its not outputing the fields (LastName, FirstName, Email, Hiredate, JobID).. Any Ideas?? Thanks Guys..:

GO
IF OBJECT_ID ('Names') IS NOT NULL
DROP VIEW Names ;
GO
CREATE VIEW Names
AS
SELECT LastName, FirstName, Email, Hiredate, JobID
FROM EMPLOYEE
GROUP BY LastName, FirstName, Email, Hiredate, JobID;
GO
Go to Top of Page
   

- Advertisement -