Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

using IN statment & a string variable. Expand / Collapse
Author
Message
Posted Tuesday, September 4, 2012 8:49 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:54 PM
Points: 13,228, Visits: 12,705
Tava (9/3/2012)
excellent, I got it working, created the Function & in the subquery now getting the results i wanted... really appreciate your help on this... no way i would have solved it.


But do you UNDERSTAND it? It is great that you have it working but unless you understand it you are only half way done. You are the person getting the phone call at 3am when your production system is down. If you don't understand it then you need to go back and read that article over and over until you do.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1353955
Posted Tuesday, September 4, 2012 9:44 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:47 AM
Points: 20,801, Visits: 32,730
Not having anything to test with, this is a possible alternative that converts your multi-statement tvf into an inline tvf. Test it out and compare it to what you already have (you may want to change the name to do some head to head comparisions).


drop function dbo.fDayCount;
go
CREATE FUNCTION [dbo].[fDayCount]
(
@WorkDate DATETIME
)
RETURNS @returntable TABLE
AS
return(
SELECT
workdate,
companyID
FROM
dbo.table1
INNER JOIN dbo.table2
ON table1.a = table2.a
WHERE
date = @date
AND companyid IN(select
ds.Item
from
dbo.config
cross apply dbo.DelimtedSplit8K(CompID,',') ds
where
configva = 'compidstring')
);
go





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1354007
Posted Tuesday, September 4, 2012 9:48 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:47 AM
Points: 20,801, Visits: 32,730
CELKO (9/3/2012)
You gave us no DDL, so this will be harder than it should. I assume that you want to get rid of this function. Good SQL never use them; they are proprietary, non-relational, screw up the optimizer and scream to the world that the author is still stuck in 1960's BASIC, right down to the “f_”, “fn_”, “udf_” prefixes that were required by FORTRAN II and BASIC.

Since we have VIEWs in SQL, a UDF that returns a table is the worst way to program SQL; but it lets BASIC avoid leaning how to do it right. Write a VIEW.

We use the DUNS for company identifiers; it is the industry standard. Can you give me a 255 character example of your company_id? When you invite garbage data with absurdly sized columns, you will get it. I also see the magic default VARCHAR(50) declarations from ACCESS.

Is the company identifier of a client or a supplier or what? In COBOL, which is what you are actually writing, the hierarchical record structure would would qualify the field; but SQL has tables with rows and columns, not records and fielsd.

I also see that you layout your code as if you were still using punch cards; each parens, data element, etc on a separate card (line). That let us re-arrange the deck on the fly. Today, we use a “pretty printer” and make the code human readable instead.

CREATE VIEW Count_Somethings_Days (work_date, client_duns)
AS
SELECT work_date, client_duns
FROM dbo.Table1, dbo.Table2 -- not real names!
WHERE Table1.vague_something = Table2.vague_something
AND client_duns IN (..);

Yes, you actually have to hard code the list, if it is what defines the set your want. Yes, I know it worked that way in 1960's BASIC, but SQL is compiled and not interpreted.

If you want this to change infrequently, put their DUNS in a table and use “client_duns IN (SELECT client_duns FROM Bankrupt_Companies)”in the VIEW. There is also the long parameter list idiom, if you want to make this into a stored procedure.

My guess is that you are about 2-3 years away from un-learning bad habits and old languages to be an SQL programmer. Keep at it and read everything you can find, starting with an intro to Set Theory and Logic. Eventually, you will get to ISO Standards which are boring as hell.



Let's see, your view may take less space, but fails in some regards. The first is the use of the old ANSI-89 style join instead of the newer ANSI-92 style join. I though you were all for using standards? You should consider updating to the newer standard. In my opinion it makes code more readable be separating the JOIN criteria from the filter criteria in the FROM clause.

Also, there really is nothing wrong with listing each column on its own line, in fact I prefer that as a coding style. Apparently so do many of the routines used to automatically format code. Personally, putting as many columns on one line leads to more difficult to read code.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1354010
Posted Tuesday, September 4, 2012 2:23 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:32 PM
Points: 35,584, Visits: 32,174
CELKO (9/3/2012)
You gave us no DDL, so this will be harder than it should. I assume that you want to get rid of this function. Good SQL never use them; they are proprietary, non-relational, screw up the optimizer and scream to the world that the author is still stuck in 1960's BASIC


Absolutely incorrect on all counts. This an Inline Table Value Function that the optimizer weaves into the code as if it were a view. "Good SQL [sic]" should always use them for this type of thing. It doesn't matter if they're proprietary because, except for the most basic of CRUD, true portability is a bloody myth. They ARE relational because they work like a view. And it would seem that the only one stuck in the 1960's is you because you didn't know this.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1354175
Posted Tuesday, September 4, 2012 3:55 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, April 27, 2014 8:38 PM
Points: 90, Visits: 420
Sean Lange (9/4/2012)
Tava (9/3/2012)
excellent, I got it working, created the Function & in the subquery now getting the results i wanted... really appreciate your help on this... no way i would have solved it.


But do you UNDERSTAND it? It is great that you have it working but unless you understand it you are only half way done. You are the person getting the phone call at 3am when your production system is down. If you don't understand it then you need to go back and read that article over and over until you do.



I know, I didn't quiet understand it all and that's just because I'm new to all this, what I did instead was (might not be the best method) took it out of the function and placed it into the Stored Proc instead.

We had the stored proc calling this function. So that was removed and code was modified.
Post #1354233
Posted Wednesday, September 5, 2012 7:32 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:54 PM
Points: 13,228, Visits: 12,705
Tava (9/4/2012)
Sean Lange (9/4/2012)
Tava (9/3/2012)
excellent, I got it working, created the Function & in the subquery now getting the results i wanted... really appreciate your help on this... no way i would have solved it.


But do you UNDERSTAND it? It is great that you have it working but unless you understand it you are only half way done. You are the person getting the phone call at 3am when your production system is down. If you don't understand it then you need to go back and read that article over and over until you do.



I know, I didn't quiet understand it all and that's just because I'm new to all this, what I did instead was (might not be the best method) took it out of the function and placed it into the Stored Proc instead.

We had the stored proc calling this function. So that was removed and code was modified.


If it were me I would put it back in the function. That way you can use it elsewhere.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1354529
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse