Solved

select view to view in sql

Posted on 2014-10-07
8
150 Views
Last Modified: 2014-10-13
I have a select statement which was written awhile back. From VB code it calls ike this :
select *from vw_sample_1

 Using cn As New OleDb.OleDbConnection(sConnString)
        cn.Open()
        Dim cmd As New OleDb.OleDbCommand("select *from vw_sample_1", cn)
        Dim r As OleDb.OleDbDataReader = cmd.ExecuteReader()

       etc...

        cn.Close


inside vw_sample_1  is

select *from vw_sample_1 s1
     inner join vw_sample_2 s2
      on s1.id   = s2.id


inside vw_sample_2

select  * from vw_sample_2
where startdate between DATEADD(day, -90, GETDATE()) and getdate()

Instead of setting  a fixed of last 90 days, I want use variable instead.  User can pass 30, 90, 60, etc.

Is there a way to pass a variable from vw_sample_1 to vw_sample_2 ?
0
Comment
Question by:VBdotnet2005
8 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40367573
No
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40367577
Views do not have parameters, you use where clauses instead.

I suspect you have substantially reduced the real sql involved, but if we take your example as

vw_sample_1  calls: vw_sample_2  and this has a hardcoded -90

Then remove that hardcoded date range from vw_sample_2

then include the wanted date range through your where clause

But I cannot be more specific unless I saw more real code (and even then I might have to see data as well).
0
 

Author Comment

by:VBdotnet2005
ID: 40367581
How about create a small table, Startdate_table.  Update Startdate field from vb code first, sample set to 30 and from vw_sample_2

select  * from vw_sample_2
where startdate between (select startdate from Startdate_table) and getdate()
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:VBdotnet2005
ID: 40367584
PortletPaul,

It was written awhile back by a previous developer. It is a long...long select statement which call view after view after view. It is very confusing. I have never seen view calls views, and views.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40367682
view of views of views: then it's almost invariably going to be less than optimal.

You could use a table - but!!! what about concurrent use?

What isn't well understood is you don't need a parameter to make things efficient in SQL. Parameters have there place in the world and they can be used with functions and stored procedures - but not with views. (and views should not need them either).

Without more details I can only give you generic answers.
0
 
LVL 47

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40367757
Why don't you change the code?
(...)
Dim cmd As New OleDb.OleDbCommand("select * from vw_sample_1 where startdate between DATEADD(day, -30, GETDATE()) and getdate() ", cn)
(...)

Open in new window

I posted an example with 30 days but you can easily transform it in a variable and use the variable instead of a constant.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40368166
instead of a View, you can create a User Defined Function that accepts parameters
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40376436
"Views do not have parameters, you use where clauses instead."
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 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