473,493 Members | 4,319 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Anyway to declare and use variables in a View

I have some SQL code I wrote that returns about 15,000 rows (using a
group by statement, raw data is around 1mill rows). In this code I
have a few variables that I declare and set. These variables for the
most part are just calling functions that return a set value that I
use later in my script in Where statements. The script runs in about
11sec and all the data looks good.

Now I would like to create a view with this script, my ultimate goal
here is to have Excel call the view for an end user that will update
whenever they want. I want to make it as easy as possible on them.

So my question is, is there any way that I can do this?

Now I can certainly just replace all of my variables. However because
of the way that SQL works and the way that I have the script
constucted it fires the functions 1 time each then just uses the
result, if I replace all the variables with the functions it has to
fire the functions around 1mill times each. These functions were
written by someone else and they work but I do find them slow.
Consequently the time to run the script jumps from 11sec to over 15min.
Jun 27 '08 #1
2 84567
This sounds like you are confusing a spreadsheet and procedural code
with a VIEW. In SQL, we don't like function calls and local
variables. We prefer declarative code and queries. For example, I
can write a VIEW like this:

CREATE VIEW InvoiceHeaders (customer_nbr, order_tot, ..)
AS
SELECT customer_nbr,
SUM(order_qty * unit_price),
..
FROM Invoices AS I, InvoiceDetails AS D
WHERE I.invoice_nbr = D.invoice_nbr
GROUP BY customer_nbr;

If you post what you have, we can do better.
Jun 27 '08 #2
You cannot declare and use variables in views. Maybe it is possible to
change your SQL statement so it does not call those functions for each row.

An alternative is to use a table valued function, in which you can declare
and use variables:
http://msdn.microsoft.com/en-us/library/ms191165.aspx

Then a view can reference the table valued function as regular table.

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
6683
by: Andy Fish | last post by:
Hi, can anybody put forward a sensible argument javascript's behaviour of creating a new global variable whenever I assign to a previously undeclared variable. I can't beleive this is just for...
5
4702
by: John Salerno | last post by:
Here's my full code: using System; using System.Collections.Generic; using System.Text; using System.IO; namespace PatternMatcher { class Program
6
3511
by: Jakob Bieling | last post by:
Hi, I recently noticed that my compiler (VC++7.1) allows me to declare variables inside the expression for an if-statement, like so: if (int i = 0) { i = 1; }
2
1572
by: Sebastian Moderlak | last post by:
Hi, while trying to read data from a table assigning values into more than one value I stopped at the following problem: declare c1 cursor for select val1 into cval1, val2 into cval2 from...
3
1616
by: stevong | last post by:
Hi, I've a custom user control: 'user1.ascx <script language="VB" runat="server"> Public str1 as string Dim conn as System.Data.SqlClient ' But this line hits and error. What should I do?
5
2167
by: dancer | last post by:
Using ASP.net 1.1 and VB Is it possible to declare variables in their own subroutine? I had my DIM statements within a sub that worked just fine. I wanted to be able to use them in another...
1
2328
by: Jean | last post by:
function myFunction(){ document.write('var path="../images/2008/'); } I use this kind of function to declare the variable path. It's on a extern javascript file. If I use this function in an...
0
1356
by: Art2010 | last post by:
Hi, There is a method (I guess this is a method) DrawArrowUp("tag1", true, 0, Low - TickSize, Color.Red); in C# where "tag1" is user defined unique id used to reference the draw object. I do not...
0
6980
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6862
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1397
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.