Multiple Unions and functions

I have a function that has 4 unions.

The queries are similar except for the where clauses

It looks something like:

SELECT...
FROM...
JOIN...
WHERE @Arg1 = 0 and @Arg2 = 0 and e.EID IS NULL
UNION
SELECT...
FROM...
JOIN...
WHERE @Arg1 = 0 and @Arg2 = 1
UNION
SELECT...
FROM...
JOIN...
WHERE @Arg1 = 1 and @Arg2 = 0 and e.EID IS NULL
UNION
SELECT...
FROM...
JOIN...
WHERE @Arg1 =1 and @Arg2 = 1

@Arg1 and @Arg2 are passed in and are 0 or 1.

Is this the best way to handle the possible combinations or would it be 
better to do an if test and only do the one that matches what was passed it.

I wasn't sure if the Optimizer might do that anyway.

Thanks,

Tom 


0
tshad
3/31/2010 12:36:35 AM
sqlserver.programming 1873 articles. 0 followers. Follow

2 Replies
545 Views

Similar Articles

[PageSpeed] 0

See Erland's article on the topic:
http://www.sommarskog.se/dyn-search.html

-- 
Plamen Ratchev
http://www.SQLStudio.com
0
Plamen
3/31/2010 1:06:53 AM
It's a good article but not sure if it really answered my question.

After playing around with it, I think I'll just leave it as it is.

The table function would have to be changed to specifically define the table 
(which it doesn't do now) and just returns what is in the select statement - 
which you can't do if you want to have if statements.

Not sure it would be better anyway.

I assume the optimizer is smart enough to see the WHERE clauses and will 
only pick the one it needs - since only one will ever be executed when the 
function is called.

Thanks,

Tom

"Plamen Ratchev" <Plamen@SQLStudio.com> wrote in message 
news:pNadneCLHc-vPS_WnZ2dnUVZ_hCdnZ2d@speakeasy.net...
> See Erland's article on the topic:
> http://www.sommarskog.se/dyn-search.html
>
> -- 
> Plamen Ratchev
> http://www.SQLStudio.com 


0
tshad
3/31/2010 1:30:42 AM
Reply:

Similar Artilces:

Which function to use?
Can someone point me to which function to use to ascertain the following If a cell contains 1 then I would like the results cell to show 20% of yet another cell 2 then I would like the results cell to show 10% of yet another cell 3 then I would like the results cell to show 6.33% of yet another cell Much obliged if you can help Hello, =CHOOSE(A1,B1*20%,C1*10%,D1*6.33%) Regards, Bernd In C1............ =LOOKUP(B1,{1,2,3},{0.2,0.1,0.0633})*A1 Gord Dibben MS Excel MVP On Sun, 26 Aug 2007 20:46:10 +0100, "Harry Limey" &l...

Create a list with multiple criteria
I have a data base with 1,000's of addresses w/zipcodes (Sheet 1 columns A to E, E being zipcodes). After setting a base address I get all of the zipcodes within a given radius (in this case 70 zipcodes), listed in Sheet 2, Column A. Next, I use COUNTIF (in column B) to find how many addresses are in each zipcode, in this example there are 46 addresses within the 70 zipcodes. What I want to do is create a list in sheet three that will list each with the data from Sheet 1 columns A to E). Any help would be appreciated. Ronbo ...

Removing multiple hyperlinks
I've got data copied into my spreadsheet that includes hyperlinks. Ther are thousands of these in a column. I can remove individually by right-clicking and selecting Hyperlink an Remove. I can then arrow down and do a control Y, cell by cell, What I want to do is highlight the column and be able tp remove th hyperlinks all at once. I've exhausted myself peering through knowlegebases and groups, bu cannot find any way to make this work. Thanks for any input you can provide -- Message posted from http://www.ExcelForum.com Hi one way: Use the following macro to remove hyperlinks...

Charting multiple multiples
Hopefully I can explain this in a way that will be understood. Data: Place A Place B Place C FWD Back FWD BACK FWD Back Org_1 1 2 3 2 1 3 Org_2 2 5 4 6 1 4 I would like to place the data so that FWD and Back data is stacked on top of each other in different colors, and placed in one row on a 3-D chart as Org-1 and a second row as Org_2. Any ideas? Thanks. If you arrange your data like this: Back FWD Org_1 Place A 2 1 Place B 2 3 Place C 3 1 Org_2 Place A 5 2 ...

Multiple Hyperlinks In Same Shape Doesn't Work
Can anyone tell me how I can get multiple hyperlinks in the same shape to work? I have no problem inserting the hyperlinks, but they all point to only one. I scoured these posts, and found this (http://groups.google.com/groups?q=hyperlink+group:microsoft.public.visio.*&hl=en&lr=&ie=UTF-8&oe=UTF-8&group=microsoft.public.visio.*&selm=3eea6467%241%40news.microsoft.com&rnum=2), which doesn't appear to work. Thanks in advance-- RAD Um, which version of Visio are you using? As far as I know, at least Visio 2002 and Visio 2003 support multiple hyperlinks. Can't...

about function keys
This is a multi-part message in MIME format. ------=_NextPart_000_0149_01CA656F.25EEE970 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable I went out a got a new keyboard. guess what? the function keys don't work on the new one. could this be anything with windows 7 by any chance????? bonnie ------=_NextPart_000_0149_01CA656F.25EEE970 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HT...

Multiple Profiles, multiple accounts, multiple machine configuring?
Is there a vb or related method of adding *both* profiles *and* email accounts to multiple users logins from the administrator account? I have a network which is a workgroup (NOT a domain), in which there are about 15 users that are replicated on each of 5 machines. I'd like to be able to add all 15 Mail Profiles, and all 15 email accounts to each of the 15 logins on these 5 machines--obviously with as little work as possible. The mail is all retrieved from the same Exchange Server, but these machines *cannot* be added to a domain (don't ask!). I'd hate to have to log into ...

distributing data into multiple cells
hii i have to prepare an exel sheet of some 2000 products..i have to copy d data from the net and then prepare the sheet,with 4 different columns for 4 diff types of entries..now when i copy data nad paste is as text.it all goes in a single cell in a row .how can i distribute the data l into multiple cells????????? -- jaspreet ------------------------------------------------------------------------ jaspreet's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=23821 View this thread: http://www.excelforum.com/showthread.php?threadid=374772 can you show some samp...

Single click selects multiple cells
When clicking on a single cell multiple cells are selected. The one time solution for this is to zoom in or out. This is problematic as 60% seems to be the zoom that works most of the time but at this zoom level the cell contents do not display. The time lost and the frustration that builds is killing my productivity and office attitude. Please give all of us a permanet fix. -- Thanks Mike ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. To vote for this suggestion, click the "I Agree" butt...

Is there a way to sort multiple columns with a tab or something?
I have a 4 column spreadsheet. I want to be able to click the heading for each of the columns to hav them sort by that column if clicked. How do I do that -- Message posted from http://www.ExcelForum.com Hi why not used the soprt icon for this. Note: This could screw up your sorting if Excel does not recognize your database columns correctly -- Regards Frank Kabel Frankfurt, Germany > I have a 4 column spreadsheet. > > I want to be able to click the heading for each of the columns to have > them sort by that column if clicked. How do I do that? > > > --- > Mess...

Sending to multiple addresses
How do I send an email to multiple addresses having only the recipient see their own email address? Michelle wrote: > How do I send an email to multiple addresses having only > the recipient see their own email address? Put the addresses into the BCC: field. Note that many e-mail programs will decide such mails are spam though. >-----Original Message----- >How do I send an email to multiple addresses having only >the recipient see their own email address? >. Open your new email and go to tools, select recipient, when your address book opens select (highlight) the a...

multiple selection
How can I select multiple records into a list box? I want that for append identical records (for select records) in another table. Many thanks Florin Use a subform instead of a list box. In a subform bound to a related table, you can append as many rows as applicable. It is possible (but not simple) to use an unbound multi-select list box that is populated in the Current event of the form, and executes Append, Delete, and Update query statements based on the ItemsSelected if they are changed. You will also have to find a way to respond to the user "undoing" changes. -- Al...

Exchange 2000 Distribution Lists
Hello We are running an Exchange 2000 environment in Exchange Native Mode and Windows Mixed Mode. We just converted from Exchange 5.5 Is there any way to configure multiple people to be owners or editors of the distribution lists? This was possible in 5.5, but we cannot find a means of accomplishing this in 2000 Any help would be appreciated Thank yo Jason "Jason P.S." <jason.pruden-shebaylo@investorsgroup.com> wrote: >We are running an Exchange 2000 environment in Exchange Native Mode and Windows Mixed Mode. We just converted from Exchange 5.5. > >Is there any w...

IIF Function 03-12-08
I need to use an If statement in a field in my query, here's what I need it to do: If field name "PYE" equals "12/31" then insert "1/1" or If field name "PYE" equals "11/31" then insert "12/1" or If field name "PYE" equals "10/31" then insert "11/1". So far I have this Exp: IIF([PYE]= "12/31", "1/1", "") it works for the PYE's that are 12/31, but I can't figure out how to use the or part. Any suggestions? Use nested IIFs like this --- IIF([PYE]= "1...

Need to have a dropdown list containing multiple rows
I am setting up an form and need to create a dropdown list that will contain 8 sets of data. Each set needs to contain 3 rows of data (name, street address & city/state/zip. The goal is to allow the user to click onto the cell, see the dropdown and select which address set to use. This would then place the data into three rows suitable for mailing. I have never attempted anything like this and am in need of assistance. Thank you. --- Message posted from http://www.ExcelForum.com/ Pat What you ask is easily done using Data Validation drop-down list and a few Vlookups but........Ex...

Combine multiple tables into one
I took over a database in which the last person created a new table for everyday worth of data (ie. Feb-21-08 is named 022108). There are over 100 tables in the single database with the same columns and table attributes, I would like to combine all of these tables into one where I have a Date_Key field in place of the table name. I figured out how to query the table names using the following: SELECT MSysObjects.Name FROM MsysObjects WHERE (Left$([Name],1)<>"~") AND (Left$([Name],4) <> "Msys") AND (MSysObjects.Type)=1 ORDER BY MSysObjects.Name; Now I j...

Multiple domains #6
Hi, Having done some research I have found that it is possible to send and receive email from multiple domains with Exchange 2003. I have a few questions that I would like cleared up if anyone can help. 1. Is it really as simple as configuring a recipient policy for the new domain and then changing the mail record for that domain to point to the proper IP address? 2. If I set up a new account in AD, and need this account to be associated with the second email domain, will the default SMTP email be for the first domain? If so, can I just edit this directly? 3. If I set the default SMTP add...

Form Field/Function problem
I have 4 fields on my main form. The 4th field is controlled by a function. The function automatically adds the scores from the other 3 fields and puts the total score in the 4th field. All of that works great. The problem I'm having is that I need the color on that 4th field to change if any of the other 3 fields are below 60 points. I've tried adding some code that would change the color, under the 'After Update' and 'After Change' property field and I can't get it to work at all. Thank you in advance for any help! Place a procedure like this air co...

Function for different array
Hi all, I need help on writing formula for the problem below: Column A Column B Column C CHJ 15 AGB AGB 10 HHH JKX 12 NNN MNB 19 MMM AGB 20 BBB . . . . . . . . . I want to be able to write " If one or more value in...

forwarding mail to multiple external addresses
I have a user on our Exchange Server 2003 that wants his email to be forwarded to 2 different external email addresses and not have the original mail stored in his exchange mailbox at all. I can set this up to froward to only one external email address by creating a contact in Active Directory with an external email address and then forwarding to that contact. However, it won't allow me to forward to multiple contacts. Please help as this seems like a pretty common request from several users. I'm not sure, as I haven't tested it before, but you might be able to create a ...

Finding Data in multiple worksheets
Using Excel 2000, how can I find data in multiple worksheets; example: any number between 200 & 299 in 32 worksheets? If you mean you want to find a single number across 32 worksheets, group your worksheets (click on the first, ctrl-click on subsequent (or even shift-click)). Then Edit|Find But I think I'd use Jan Karel Pieterse's FlexFind: http://www.oaltd.co.uk/MVP/ If you meant you wanted to find all 100 numbers (200-299) in all 32 sheets, then I think I'd have a macro search for each number and report the results on a new worksheet. "mark@southwestconst.com&quo...

is there an auto complete function for words or phrases in 2007
I was 'upgraded' to 2007 from 2003 and am not happy about it. I have lost all my auto complete words and phrases; some of them paragraphs. Can anyone help me find out if this function is a possibility in word 2007? Many thanks. K AutoComplete doesn't work in Word 2007 (except for dates), but you can still insert AutoText entries by typing the first few letters and pressing F3. Or you can convert them to AutoCorrect entries. -- Suzanne S. Barnhill Microsoft MVP (Word) Words into Type Fairhope, Alabama USA http://word.mvps.org "Karen" <Karen@d...

Which Function does....
I have a range of cells (about 15 rows (people) and 180 columns(dates)). I am using max to identify the highest value within that range. Now that I have that many cells, which function would I use to identify the row (row 1) and column header (colmn A) labels associted with that max value? did this macro for a different problem a few days back. It should work for you though If your max function is in A18 the result will be in B18 and C18 Sub testmax() Dim myrange As Range Set myrange = Range("A1:FY15") Range("B18").Activate For Each cell In myrange If cell.Value = Ra...

show multiple values that add to total in bar graph
Is there a way to show multiple values in a single bar (in a bar graph) that add up to a total (example: 127, 129, 87, 85, 60, 126, 89 add up to 703)? I want a sigle bar that goes to 703 but breaks the single bar down into these segments (preferrably by color showing the smaller segment amounts within the color. HELP!!!! Thanks! Hi, Is that not what the stacked column chart does? Cheers Andy Green Goose 89 wrote: > Is there a way to show multiple values in a single bar (in a bar graph) that > add up to a total (example: 127, 129, 87, 85, 60, 126, 89 add up to 703)? I > want...

Function of Print To Slip receipt option?
Hello, I've been looking for what exactly "Print To Slip (OPOS)" on the receipt properties window in RMS Manager does, and I have yet to find an answer. Can anyone give me an explanation? Thank you. Data, If you have a slip receipt printer, it will print info on the slip receipt instead of the smaller 40 paper. Some examples of slip printers; http://tinyurl.com/rahka http://tinyurl.com/o5jbp -- * Get Secure! - www.microsoft.com/security You must be using Outlook Express or some other type of newsgroup reader to see and download the file attachment. If you are not usi...