SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


View with EXISTS


View with EXISTS

Author
Message
ikrami2000
ikrami2000
SSC Veteran
SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)

Group: General Forum Members
Points: 229 Visits: 416
The below view is not work .....

Create View XYZ As

IF Exists
(
select Code from PR_Header where Code = (Select 'PR-'+''+ CAST(Count(a.ID)+1 as varchar ) from PR_Header a Group by a.Company)
)
Begin
Select 'PR-'+''+ CAST(Count(ID)+2 as varchar ) as PR_PR ,'Default PR',Company from PR_Header Group by Company
End
Else
Select 'PR-'+''+ CAST(Count(ID)+1 as varchar ) as PR_PR ,'Default PR',Company from PR_Header Group by Company
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60713 Visits: 13297
I don't think you can use the IF...ELSE construct in a view.
You'll need a stored procedure for that.

The error might have given this away:


Msg 156, Level 15, State 1, Procedure Test, Line 3
Incorrect syntax near the keyword 'IF'.



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
ikrami2000
ikrami2000
SSC Veteran
SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)

Group: General Forum Members
Points: 229 Visits: 416
i already got the error , but i need to store this values in a view
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60713 Visits: 13297
ikrami2000 (10/16/2013)
i already got the error , but i need to store this values in a view


You cannot "store" values in a view.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Sean Lange
Sean Lange
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60827 Visits: 17954
Not entirely sure what you are trying to do here but you don't need an IF statement to do this type of thing.

See if this will get what you are looking for.


CREATE VIEW XYZ
AS
SELECT 'PR-' + '' + CAST(Count(ID) + CASE
WHEN EXISTS (
SELECT Code
FROM PR_Header
WHERE Code = (
SELECT 'PR-' + '' + CAST(Count(a.ID) + 1 AS VARCHAR)
FROM PR_Header a
GROUP BY a.Company
)
)
THEN 2
ELSE 1
END AS VARCHAR) AS PR_PR
,'Default PR'
,Company
FROM PR_Header
GROUP BY Company



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
ikrami2000
ikrami2000
SSC Veteran
SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)SSC Veteran (229 reputation)

Group: General Forum Members
Points: 229 Visits: 416
Thanks :-) , this is exactly what i want
Sean Lange
Sean Lange
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60827 Visits: 17954
ikrami2000 (10/16/2013)
Thanks :-) , this is exactly what i want


You are quite welcome. I would caution you that there are a few things in that code that I would recommend changing. You are concatenating empty strings which is totally pointless. You also have a number of cast to varchar where you don't specify the size of the varchar. Do you know what the default size for a varchar is? I can't remember either. This is why you should always add the size to your declaration.

Something like this:


CREATE VIEW XYZ
AS
SELECT 'PR-' + CAST(Count(ID) + CASE
WHEN EXISTS (
SELECT Code
FROM PR_Header
WHERE Code = (
SELECT 'PR-' + CAST(Count(a.ID) + 1 AS VARCHAR(5))
FROM PR_Header a
GROUP BY a.Company
)
)
THEN 2
ELSE 1
END AS VARCHAR(5)) AS PR_PR
,'Default PR'
,Company
FROM PR_Header
GROUP BY Company



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search