473,396 Members | 1,998 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,396 software developers and data experts.

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
Jul 31 '08 #1
3 6435
On Thu, 31 Jul 2008 09:54:03 -0700 (PDT), jd*****@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
Jul 31 '08 #2
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.
Jul 31 '08 #3
(jd*****@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, es****@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
Jul 31 '08 #4

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

Similar topics

1
by: jandoca | last post by:
Hi, I am new to crystal reports and I have to build some reports and call them from an ASP page and display them. I am using Crystal Reports Developer 9.0 and the client has Professional 9.0. I...
8
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
3
by: fabio de francesco | last post by:
Hello, I have a couple of years of experience with C++. I started studying C++ syntax, then I read the B.Stroustrup's book, and eventually I went through the N.Josuttis' book on how to program...
9
by: iceColdFire | last post by:
Hi, Is there some way where I can call a function without explicitly making a function call... I need this in a scenario where a function is called say f(); and it automatically calls some...
16
by: aarklon | last post by:
Hi folks, recently i read the book named assembly language step by step by Jeff Duntemann. in the chapter coding for linux, he has got a paragraph named C calling conventions, which he...
1
by: Nick Bishop | last post by:
I have a problem where I call a method in a C++ class with a pointer which is a static member in that class. When I use a debugger, I see the pointer having a certain value, but when I step into...
6
by: johnf401 | last post by:
I've got a VB .NET Web application that has several frames (for discussion sake, let's call them Form1.aspx and Form2.aspx). I want to be able to call a code module in Form2.aspx.vb from code...
12
by: Ron | last post by:
Greetings, I am trying to understand the rational for Raising Events instead of just calling a sub. Could someone explain the difference between the following 2 scenarios? Why would I want to...
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:
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
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
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...

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.