473,776 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

create dynamic variable in procedure

Please help. I'm stumped.

I need to create a dynamic variable in a procedure that will be used
to update a variable of the same name in a table. i.e. the name in
tblAnalysisScor es are WEAQScore0, WEAQScore1,WEAQ Score5, MissingWEAQ0,
MissingWEAQ1, MissingWEAQ5
The 0, 1, 5 are the @Interval. I am using a cursor to loop through the
table. Each row in the table is the data for one person at a given
timepoint (0,1,5).

This is what I have so far, but it doesn't recognize the concatenated
name as the name in the table.
What is the correct syntax to reference the variable?

In the code before this, I have already calculated @WEAQScore for the
current record of data. Now I want to update the correct variable in
tblAnalysisScor es

UPDATE tblAnalysisScor es SET
-- WEAQ Summmary/Missing
'WEAQScore'+ cast(@Interval as char(1)) = @WEAQScore
,'MissingWEAQ' + cast(@Interval as char(1)) = @MissingWEAQ
WHERE
SubID = @SubID
Jun 27 '08 #1
7 6587
You can avoid dynamic SQL with something like this:

UPDATE tblAnalysisScor es
SET WEAQScore0 = CASE WHEN @Interval = 0 THEN @WEAQScore ELSE WEAQScore0
END,
MissingWEAQ0 = CASE WHEN @Interval = 0 THEN @MissingWEAQ ELSE
MissingWEAQ0 END,
WEAQScore1 = CASE WHEN @Interval = 1 THEN @WEAQScore ELSE WEAQScore1
END,
MissingWEAQ1 = CASE WHEN @Interval = 1 THEN @MissingWEAQ ELSE
MissingWEAQ1 END,
WEAQScore5 = CASE WHEN @Interval = 5 THEN @WEAQScore ELSE WEAQScore5
END,
MissingWEAQ5 = CASE WHEN @Interval = 5 THEN @MissingWEAQ ELSE
MissingWEAQ5 END
WHERE SubID = @SubID;

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 27 '08 #2
On May 14, 5:01*pm, "Plamen Ratchev" <Pla...@SQLStud io.comwrote:
You can avoid dynamic SQL with something like this:

UPDATE tblAnalysisScor es
SET WEAQScore0 = CASE WHEN @Interval = 0 THEN @WEAQScore ELSE WEAQScore0
END,
* * * MissingWEAQ0 = CASE WHEN @Interval = 0 THEN @MissingWEAQ ELSE
MissingWEAQ0 END,
* * * WEAQScore1 = CASE WHEN @Interval = 1 THEN @WEAQScore ELSE WEAQScore1
END,
* * * MissingWEAQ1 = CASE WHEN @Interval = 1 THEN @MissingWEAQ ELSE
MissingWEAQ1 END,
* * * WEAQScore5 = CASE WHEN @Interval = 5 THEN @WEAQScore ELSE WEAQScore5
END,
* * * MissingWEAQ5 = CASE WHEN @Interval = 5 THEN @MissingWEAQ ELSE
MissingWEAQ5 END
WHERE SubID = @SubID;

HTH,

Plamen Ratchevhttp://www.SQLStudio.c om
Thanks for your quick response. I appreciate that. I thought of
setting the values with CASE, but I have 12 measures. The problem is
the 12 measures have up to 12 timepoints with 5 sessions within each
timepoint and some have 3 sessions within the 5 sessions. That would
be a lot of code. That is why I was hoping to do it with dynamic
variables (Hope that is the right term. I am relatively new to SQL)
Seemed like a great idea since I have the timepoint and session and
just need to concatenate those to create the variable name that is
already in the table.
I don't know what the syntax is to create a dynamic variable. Do I
have to have a recordset open for tblAnalysisScor es? I'm just
grasping here.
Jun 27 '08 #3
You cannot use variables for column names in dynamic SQL. You would have to
concatenate the SQL as a string to add the columns, and then pass the other
parameters and execute.

DECLARE @sql NVARCHAR(2000);

SET @sql = N'
UPDATE tblAnalysisScor es
SET WEAQScore' + CAST(@Interval AS CHAR(1)) + ' = @WEAQScore, ' +
' MissingWEAQ' + CAST(@Interval AS CHAR(1)) + ' = @MissingWEAQ
WHERE SubID = @SubID';

DECLARE @params NVARCHAR(100);

SET @params = N'@WEAQScore INT, @MissingWEAQ INT, @SubID INT';

EXEC sp_executesql @sql, @params, @WEAQScore, @MissingWEAQ, @SubID

Read Erland Sommarskog's article on dynamic SQL:
http://www.sommarskog.se/dynamic_sql.html

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 27 '08 #4
> The problem is the 12 measures have up to 12 timepoints with 5 sessions within each timepoint and some have 3 sessions within the 5 sessions. That would be a lot of code. <<

LOL! Why is it that traditional proceudral programmers think nothing
of writing hundreds of lines of procedural code then panic over using
a text editor to generate a long SQL query with a repetitive
structure?

What Ratchev gave you will touch the table once, avoid using a cursor
(figure ~10 to ~100 times faster) and be easy to maintain because of
the repetitive structure of the CASE expressions.
Jun 27 '08 #5
Thanks for your quick response. I appreciate that. I thought of
setting the values with CASE, but I have 12 measures. *The problem is
the 12 measures have up to 12 timepoints with 5 sessions within each
timepoint and some have 3 sessions within the 5 sessions. *That would
be a lot of code. *
It would be a lot more code, but that code will run orders of
magnitude faster than what you are doing now because it would only
have to to read from the table once, would not have the overhead
associated with cursor. The code also would be far easier to read an
maintain for anyone (including you) who had to deal with problems in
the future.
Jun 27 '08 #6
On May 15, 1:05*pm, Shuurai <Shuura...@hotm ail.comwrote:
Thanks for your quick response. I appreciate that. I thought of
setting the values with CASE, but I have 12 measures. *The problem is
the 12 measures have up to 12 timepoints with 5 sessions within each
timepoint and some have 3 sessions within the 5 sessions. *That would
be a lot of code. *

It would be a lot more code, but that code will run orders of
magnitude faster than what you are doing now because it would only
have to to read from the table once, would not have the overhead
associated with cursor. *The code also would be far easier to read an
maintain for anyone (including you) who had to deal with problems in
the future.
You have all been very helpful. This is great. Thanks!
Jun 27 '08 #7
(bp*******@life span.org) writes:
Thanks for your quick response. I appreciate that. I thought of
setting the values with CASE, but I have 12 measures. The problem is
the 12 measures have up to 12 timepoints with 5 sessions within each
timepoint and some have 3 sessions within the 5 sessions. That would
be a lot of code. That is why I was hoping to do it with dynamic
variables (Hope that is the right term. I am relatively new to SQL)
Seemed like a great idea since I have the timepoint and session and
just need to concatenate those to create the variable name that is
already in the table.
You are right, that would be a lot of code. That may reflect that this
is not the best table design for the task. Maybe those columns should
be rows instead. But without knowledge of the business domain, I don't
want to make a definitive statement on that point.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.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
Jun 27 '08 #8

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

Similar topics

4
8102
by: MD | last post by:
I am trying to create a dynamic SQL statement to create a view. I have a stored procedure, which based on the parameters passed calls different stored procedures. Each of this sub stored procedure creates a string of custom SQL statement and returns this string back to the main stored procedure. This SQL statements work fine on there own. The SQL returned from the sub stored procedure are returned fine. The datatype of the variable that...
1
9903
by: Not Me | last post by:
Hi, I'm sure this is a common problem.. to create a single field from a whole column, where each row would be separated by a comma. I can do this for a specified table, and column.. and I've created a function using VBA to achieve a more dynamic (and very slow) solution.. so I would like to implement it using a user defined function in sql server. The problems I'm facing are, that I can't use dynamic sql in a
22
2762
by: nobody | last post by:
hello everybody, is there a way of creating an array with help of a function that would accept the name of this array as a parameter and then create global Array type variable of that name? so that for example the following code would work as well in browsers as under Windows Scripting Host: str = "tableA";
4
13069
by: Tim.D | last post by:
People, I've ventured into the wonderful world of Stored Procedures. My first experience has been relatively successful however I am stuck on using host variables to specifiy actualy table or column names in a FROM clause. After many hours or reading all manner of manuals I've discovered it appears this is not possible and that in order to so I need to further venture into dynamic SQL. My present procedure is based on all static SQL...
10
1844
by: serge | last post by:
I can not create a stored procedure that calls another not yet created stored procedure? In MS SQL I get a warning that the calling procedure does not exist but the new stored Procedure gets created anyhow. I believe Oracle works similarly. I can not make this possible in DB2?
8
4921
by: peteh | last post by:
Hi All; I have what I hope is a simple environmental problem. I have some SQL that creates a VERY simple procedure. When the create is executed from Quest (from a Windows client) - no problem. As soon as I go to the command line (on the actual DB server), I get a strange (and somewhat frightening) sqlcode -1007 (corrupt tablespace!?). I'm pretty sure it has something to do with CLP/statement terminator behavior, but cannot find the recipe...
2
1724
by: Mukesh | last post by:
Hi all I m Using SQL SERVER 2005 I have a requirement to store some data in xmldatatype using stored procedure , Here is example
4
3751
by: JohnnyDeep | last post by:
I am trying to create a store proc that contain a create index with the cluster option and I receive DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned: SQL0623N A clustering index already exists on table "PGIR.TF_RRCE". LINE NUMBER=35. SQLSTATE=55012
5
7216
by: SQLJunkie | last post by:
I have a small SQL script that rotates through all databases on the server and executes a Stored Procedure in each of them. Here are the steps: 1: The first step is to get name of databases in to a cursor. 2: Build dynamic SQL to change database to database in variable - this step fails 3: Execute SP 4: Move to next database. Is it not possible to change current database using Dynamic SQL? Can someone help me here please?
0
9625
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9459
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9920
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8948
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7467
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6721
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5365
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3619
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2857
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.