Solved

select view to view in sql

Posted on 2014-10-07
8
152 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

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 50

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with SQL pivot 11 49
Run software updates from the website 6 50
T-SQL: need to reset a declared variable 4 33
How do you think this website does searches? 5 34
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

738 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