Solved

SQL View Syntax returns error 156

Posted on 2004-10-12
11
283 Views
Last Modified: 2012-06-21
Trying to create a view, when i click Check Syntax in Enterprise Manager, error 156 is returned.  The SQL statement is:

CREATE VIEW dbo.GL00100
AS
IF SYSTEM_USER = 'sa'
      BEGIN
      SELECT * FROM dbo.GL00100x
      END
ELSE
      BEGIN
      SELECT  * FROM dbo.GL00100x WHERE (USERDEF1 = SYSTEM_USER)
      END




0
Comment
Question by:jlaqualia
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 3

Expert Comment

by:pra_kumar03
ID: 12292194
A create view can not have an IF Clause
0
 
LVL 9

Expert Comment

by:apirnia
ID: 12292219
You can only do what you doing in a SP or Query Analyzer.
0
 
LVL 3

Expert Comment

by:pra_kumar03
ID: 12292224
try this instead

CREATE VIEW dbo.GL00100
AS

select * from
dbo.GL00100x
where USERDEF1  like (  case
                  when SYSTEM_USER = 'sa' then '%'
                  else char(39)+SYSTEM_USER+char(39)
end )
0
 
LVL 3

Expert Comment

by:pra_kumar03
ID: 12292274
sorry it must have not gone thru
try this

CREATE VIEW dbo.GL00100
AS

select * from
dbo.GL00100x
where USERDEF1  like (  case
               when SYSTEM_USER = 'sa' then '%'
               else ''
end )
or USERDEF1 = SYSTEM_USER
0
 
LVL 17

Accepted Solution

by:
BillAn1 earned 500 total points
ID: 12292321
a less contorted method (and a bit quicker) :

CREATE VIEW dbo.GL00100
AS

select * from
dbo.GL00100x
where SYSTEM_USER = 'sa'
or USERDEF1 = SYSTEM_USER
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 69

Expert Comment

by:ScottPletcher
ID: 12292341
This should do it more simply:


CREATE VIEW dbo.GL00100
AS
SELECT *
FROM dbo.GL00100x
WHERE SYSTEM_USER = 'sa' OR (userdef1 = SYSTEM_USER)
0
 
LVL 3

Expert Comment

by:pra_kumar03
ID: 12292349
BillAn1
your sql would not return the whole table when the system user is sa. I think what jlagulia is trying to do is to get the whole table is sa is the user else only one user.
0
 
LVL 17

Expert Comment

by:BillAn1
ID: 12292478
yes it will - as with ScottPletcher's similar answer.

where SYSTEM_USER = 'sa'
or USERDEF1 = SYSTEM_USER
if the first part of the the condition is true , i.e. SUSTEM_USER = 'sa' then you will get all rows.
otherwise, you rely on the second part of the condition, USERDEF1 = SYSTEM_USER to be true, i.e. you only get rows for that particular user if the user is not sa.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 12292488
Yeah, sorry BillAn1, didn't see your answer when I posted mine or I wouldn't have :-) .
0
 
LVL 17

Expert Comment

by:BillAn1
ID: 12292574
Hey Scott, it happens all the time, crossposting -  it's the nature of the beast.
0
 
LVL 3

Expert Comment

by:pra_kumar03
ID: 12298068
Hi scott and BillAn1
I did see your query again and I realize that is correct... sorry for overlooking.. this surely wud be a faster and better query.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
c# code 19 61
Backup Job question 4 20
SQL Server memory Issue 7 76
select over clause 1 15
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now