473,800 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using a pointer to a local varaiable in Dynamic SQL

2 New Member
I am having trouble with dynamic SQL.
Early in my code I assign data to a bunch of local variables.
I want to access these later in my code and use the data values.
The code example below shows a simplified example to explain what I am trying to do.

-- ----------------------------------------------
-- Declare and set the data into a local variable
-- ----------------------------------------------
DECLARE @SD1 real
SET @SD1 = 1.1

-- ----------------------------------------------------------
-- Declare and set a variable to point to data local variable
-- ----------------------------------------------------------
DECLARE @SDName varchar
SET @SDName = '@SD1'

-- ---------------------------------------
-- Declare and set the dynamic SQL command
-- ----------------------------------------
DECLARE @SQLCmd varchar
SET @SQLCmd = 'SELECT MyNumber = ' + @SDName

By running this code the @SQLCmd contains the following ...
SELECT MyNumber = @SD1

BUT what I REALLY want is for @SQLCmd to contain this ...
SELECT MyNumber = 1.1

How can I accomplish this?
Nov 6 '09 #1
4 1546
ck9663
2,878 Recognized Expert Specialist
Why do you need another variable? Can't you just do a

Expand|Select|Wrap|Line Numbers
  1. DECLARE @SQLCmd varchar
  2. SET @SQLCmd = 'SELECT MyNumber = ' + cast(@SD1 as varchar(10))
  3.  
Happy Coding!!!

--- CK
Nov 6 '09 #2
RakuRay
2 New Member
I appreciate your answer and quick response but sorry to say that your code is using the @SD1 local variable directly. What I want to do is use the @SDName variable that is equated to the name @SD1. I need to do this because the actual @SD1 variable name will change within a loop so that @SDName could be equal to @SD1 or @SD2 or ... @SD500 at some point.

Thanks
Nov 6 '09 #3
ck9663
2,878 Recognized Expert Specialist
Could you post some portion of the loop? There might be other way of doing it.

But if you insist on doing it, try this technique:

Expand|Select|Wrap|Line Numbers
  1.  
  2. set nocount on
  3. declare @x1 int, @x2 int, @x3 int, @varname char(3)
  4. declare @sqlstatement  varchar(max)
  5.  
  6. select @x1 = 1, @x2 = 2, @x3 = 3
  7. set @varname = '@x3'
  8.  
  9. select @varname 
  10. set @sqlstatement = 
  11.  ('
  12.  
  13. declare @x1 int, @x2 int, @x3 int, @varname char(3)
  14.  
  15. set @x1 = ' + cast(@x1 as varchar(5)) + ' 
  16. set @x2 = ' + cast(@x2 as varchar(5)) + ' 
  17. set @x3 = ' + cast(@x3 as varchar(5)) + ' 
  18.  
  19. set @varname = ' + @varname + 
  20. '
  21. select @varname
  22.  
  23. ')
  24.  
  25. exec (@sqlstatement)
  26.  
  27.  
Try playing around the value of:

Expand|Select|Wrap|Line Numbers
  1. set @varname = '@x3'
  2.  
Happy Coding!!!


--- CK
Nov 6 '09 #4
nbiswas
149 New Member
Instead of SET @SDName = '@SD1' use
SET @SDName = @SD1
. No quotes
Nov 9 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
1576
by: andylcx | last post by:
Hi all, I have a question about the code below. Any idea abuot that, thanks in advance! myclass.h class myclass { public: myclass(); ~myclass(); void setdata();
7
5196
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving this. Ok... here goes.... I have a function that passes a pointer to a string to another function. For example: int FunctionA ()
4
3631
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
10
1573
by: masood.iqbal | last post by:
The code example below shows the dynamic allocation of a 2D array. I must admit that it took quite a while for me to get there (I already have another posting to that effect), but I am glad that I finally got it working. Now here's the problem: I am able to get the 2D array dynamically allocated correctly as long as I am doing it "in-line" (i.e. without invoking any function). The moment I try to do it in another function, I get a...
42
5634
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
8
2006
by: Cuthbert | last post by:
Hi folks, This question is a little deep into memory management of microprocessor. How do we know the memory arrangement using in microprocessors? Top-Bottom or Bottom-Top? For example, the "Top-Bottom" is arranging memory resource from higher address to lower address.
6
3681
by: semkaa | last post by:
Can you explain why using ref keyword for passing parameters works slower that passing parameters by values itself. I wrote 2 examples to test it: //using ref static void Main(string args) { List<TimeSpantimes = new List<TimeSpan>(); DateTime start; DateTime end; for (int j = 0; j < 1000; j++)
17
3240
by: djcredo | last post by:
Hey all, I want to return a pointer to a struct. Here is what I'lm trying to do: struct Position{ int x; int y; }; Position* GraphicTag::getRenderCentre(){
6
2528
by: worlman385 | last post by:
For pointer and non-pointer initialization of an object like MyCar mycar; MyCar* mycar = new MyCar(); I heard from other people saying if object i create must live outside scape, then I use pointer version, else if only use object for a limited scope, then use non-pointer version. Does limited scope means the object is only used in the same function
0
9691
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
10276
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9090
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
7580
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
6813
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
5471
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
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
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.