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


Creating a view with Declare


Creating a view with Declare

Author
Message
bawinkley
bawinkley
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 42
I am trying to create a view with the below query.
it should be a view as data in other tables will be updated and I want to refer to this often. Or as a last resort schedule the creation of this table over night some how?

DECLARE @@string1 varchar (20) = ' Number='
DECLARE @@string2 varchar (20) = ' And Id='

SELECT f.linkid, f.docname, f.title, dc.catname, t.template, r.extlink ,f.libid

,substring(extlink, charindex(@@string1, extlink)+len(@@string1),
charindex(@@string2, extlink) -len(@@string1) - charindex(@@string1, extlink)) as ID

from AL_TEMPLATE t
INNER JOIN AL_RELATION r ON t.tempID=r.tempid
INNER JOIN AL_RELATED_FILE rf ON r.relID=rf.relid
INNER JOIN al_file f ON rf.linkid=f.linkID
INNER JOIN al_filecats fc ON f.linkid=fc.linkid
INNER JOIN AL_DOCCAT dc ON fc.catid=dc.catID

where charindex(@@string2, extlink) -len(@@string1) - charindex(@@string1, extlink) > 0

Thanks
GilaMonster
GilaMonster
SSC Guru
SSC Guru (220K reputation)SSC Guru (220K reputation)SSC Guru (220K reputation)SSC Guru (220K reputation)SSC Guru (220K reputation)SSC Guru (220K reputation)SSC Guru (220K reputation)SSC Guru (220K reputation)

Group: General Forum Members
Points: 220685 Visits: 46279
A view can only contain a single select statement, no other statements are allowed.

Try an in-line table-valued function with those two variables as parameters to the function.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


bawinkley
bawinkley
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 42
I'm pretty sure you cant use variables in views, I have decided to take them out which isn't the end of the world.

I haven't worked out how to create a table on a scheduled task yet but i think the above method is better for this purpose.


Thanks
bawinkley
bawinkley
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 42
sorry GilaMonster, didn't see your response,

um - ok I'll have a look at that thanks!
kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5262 Visits: 2767
bawinkley (7/23/2013)
I'm pretty sure you cant use variables in views, I have decided to take them out which isn't the end of the world.

I haven't worked out how to create a table on a scheduled task yet but i think the above method is better for this purpose.


Thanks

As Gail already suggested you can use inline table valued function and can pass those two parameteres in that..........

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
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