Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > Databases > General SQL Server Support

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-31-2008, 05:54 PM
jduhler@hotmail.com
 
Posts: n/a
Calling Commonly Used SQL (reusable code)

Is there a way in SQL Server T-SQL to store commonly used SQL
statements in a function, stored proc, or system variable? That way
if that code ever changes I can change it in one place.

If I use the code below in 100 different SQL statements... can I
stored it somewhere and just call it from another SQL statement?

For example, I want to call this very simplified statement 100 times
from different stored procs::

(SELECT A FROM B WHERE C=@MYVAR) FIELDVALUE

I have tried using a function such as:

CREATE FUNCTION myfunction (@MYVAR int)
RETURNS VARCHAR(2000)
AS
BEGIN
RETURN 'SELECT A FROM B WHERE C=' + @MYVAR + ' FIELDVALUE'
END

Then I attempt to call that function from another stored procedure
such as:

CREATE PROCEDURE CallingProcedure
@MYVAR INT
AS
BEGIN

SELECT
dbo.myfunction(@MYVAR)
,(SELECT ANOTHERFIELD FROM ANOTHERTABLE) ANOTHERFIELDVALUE

END

When I run CallingProcedure, it shows the field name returned from the
function as "(SELECT A FROM B WHERE C=@MYVAR) FIELDVALUE" with a
value of null. I want it to be able to show the select statement, so
that I can see a field named "FIELDVALUE".

Thank you

-JD
Reply With Quote
  #2 (permalink)  
Old 07-31-2008, 07:21 PM
Hugo Kornelis
 
Posts: n/a
Re: Calling Commonly Used SQL (reusable code)

On Thu, 31 Jul 2008 09:54:03 -0700 (PDT), jduhler@hotmail.com wrote:

(snip)
>I have tried using a function such as:
>
>CREATE FUNCTION myfunction (@MYVAR int)
> RETURNS VARCHAR(2000)
>AS
>BEGIN
> RETURN 'SELECT A FROM B WHERE C=' + @MYVAR + ' FIELDVALUE'
>END


Hi JD,

You are telling SQL Server to return a string. SQL Server will do that,
without bothering to see if that string happens to be a valid query.

If you want to return the result of the query, you'll have to execute
it, store the result, and than return the result.

CREATE FUNCTION dbo.myfunction (@MYVAR int)
RETURNS VARCHAR(2000)
AS
BEGIN;
DECLARE @Res vanrchar(2000);
SET @Res = (SELECT A FROM B WHERE C = @MYVAR);
RETURN @Res;
END;

Note however that encapsulating reused code in seperate functions may
save on maintenance, but you pay the price in performance. SQL Server
can no longer optimize the queries as good as it would otherwise do.
Take for instance this query:

SELECT Col1, Col2
FROM SomeTable
WHERE SomeColumn = @SomeValue;

You can consider to "refactor" this to use two functions, one to fetch
the Col1 value and one to fetch the Col2 value, like this:

SELECT dbo.GetCol1FromTable(@SomeValue) AS Col1,
dbo.GetCol2FromTable(@SomeValue) AS Col2;

But the result will be that SQL Server has to access the same table
twice, reading the same row both times. You have just doubled execution
time!

And it gets even worse if joins are involved.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Reply With Quote
  #3 (permalink)  
Old 07-31-2008, 08:29 PM
--CELKO--
 
Posts: n/a
Re: Calling Commonly Used SQL (reusable code)

It is a little hard to tell what you want from the skeleton code you
posted, but SQL has VIEWs. These are "virtual tables" and they are
declared by

CREATE VIEW <view name> [(<column name list>)]
AS
<table expression>; -- usually a SELECT statement

You use them as if they were base tables in your queries. You can drop
or alter a view, so that the next time it is referenced, any changes
will be in effect.
Reply With Quote
  #4 (permalink)  
Old 07-31-2008, 10:47 PM
Erland Sommarskog
 
Posts: n/a
Re: Calling Commonly Used SQL (reusable code)

(jduhler@hotmail.com) writes:
> Is there a way in SQL Server T-SQL to store commonly used SQL
> statements in a function, stored proc, or system variable? That way
> if that code ever changes I can change it in one place.
>
> If I use the code below in 100 different SQL statements... can I
> stored it somewhere and just call it from another SQL statement?
>
> For example, I want to call this very simplified statement 100 times
> from different stored procs::
>
> (SELECT A FROM B WHERE C=@MYVAR) FIELDVALUE


Unfortunately very simplified examples from your real-world chores
can be very misleading. It's not at all really clear to me what
you want to achieve.

But generally, T-SQL as a general programming language is not as powerful
as a modern object-oriented language. Its strengths lie elsewhere.

> I have tried using a function such as:
>
> CREATE FUNCTION myfunction (@MYVAR int)
> RETURNS VARCHAR(2000)
> AS
> BEGIN
> RETURN 'SELECT A FROM B WHERE C=' + @MYVAR + ' FIELDVALUE'
> END


As I said, I don't really understand what you are trying to achieve,
but it looks you like you could make use of a preprocessor. No, there
is no such thing in SQL Server, but if you have access to C++, running
the SQL code through the C preprocessor and then load it with SQLCMD
is not that difficult.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > Databases > General SQL Server Support


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:37 AM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Best Credit Cards | Mortgage | Remortgages | Debt Consolidation | Mortgage Calculator



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114