473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 84468
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.