473,568 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stupid Problem - Please help

Hi! I would be grateful for any advise regarding what I'm doing wrong.. My
brain is stuck. Probably some stupid simple mistake I don't see. Thanks very
much for your efforts!

Martin

I have this code:

DECLARE
@ContactID varchar(10),
@AddressID int,
@cmdSQL varchar(500)

Set @ContactID = '12'

SET @cmdSQL = 'SELECT AddressID FROM i2b_ash_contact WHERE ContactID = ' +
@ContactID

EXEC (@cmdSQL)

But how do I store the value returnd by EXEC (@cmdSQL) in the variable
@AddressID????

Tried several things unsucessfully e.g.:

EXEC @AddressID = @cmdSQL
SET @ AddressID = EXEC(@cmdSQL)

Am I really so stupid that I can't see what I'm doing wrong???
Jul 20 '05 #1
7 1742
[posted and mailed, please reply in news]

Martin Feuersteiner (th************ @hotmail.com) writes:
I have this code:

DECLARE
@ContactID varchar(10),
@AddressID int,
@cmdSQL varchar(500)

Set @ContactID = '12'

SET @cmdSQL = 'SELECT AddressID FROM i2b_ash_contact WHERE ContactID = ' +
@ContactID

EXEC (@cmdSQL)


The first question is: why use dynamic SQL at all for the above?

SELECT @adrid = AddressID FROM i2b_ash_contact WHERE ContactID = @ContactID

is the simple way.

In case you really need to use dynamic SQL, sp_executesql is the way
to go. But there is a fair chance that even if your actual problem is
more complex, that you should not be using dynamic SQL at all.

Anyway, if you want to learn more about dynamic SQL, I have an article
on my web site, http://www.sommarskog.se/dynamic_sql.html. If you are
in a hurry, the solution to your problem is at
http://www.sommarskog.se/dynamic_sql.html#sp_executesql.
--
Erland Sommarskog, SQL Server MVP, so****@algonet. se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
You don't need EXEC to do this.

SET @AddressID =
(SELECT AddressID
FROM i2b_ash_contact
WHERE ContactID = @ContactID)

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #3

"David Portas" <RE************ *************** *@acm.org> wrote in message
news:fN******** ************@gi ganews.com...
You don't need EXEC to do this.

SET @AddressID =
(SELECT AddressID
FROM i2b_ash_contact
WHERE ContactID = @ContactID)

--
David Portas
------------
Please reply only to the newsgroup
--


Thanks everyone for replying. Well, I haven't mentioned that my table name
is dynamic, something like:

DECLARE
@AddressID int,
@ProgClient (varchar(10),
@Table varchar(10)

SET @Table = 'i2b_ + @ProgClient + '_contact

SET @AddressID = (SELECT AddressID FROM @Table WHERE ContactID = @ContactID)
I need to evaluate @Table first otherwise it can't find it.
Jul 20 '05 #4
> I need to evaluate @Table first otherwise it can't find it.

Unless you make it just one table and put the Progclient value in as a
column. Seems like an unusual design to have multiple tables of Contact
details.

CREATE TABLE Contacts (progclient VARCHAR(10), contactid INTEGER, addressid
INTEGER NOT NULL, ... PRIMARY KEY (progclient, contactid))

SET @AddressID =
(SELECT AddressID
FROM Contacts
WHERE ContactID = @ContactID
AND progclient = @ProgClient)

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #5
Thanks for your help Dave.
I've to admit, it's an unusual design but I've multiple contact tables named
e.g. i2b_ash_contact or i2b_ted_contact .
i2b_ and _contact are static but the middle part is dynamic. Reason for this
design is that I've a web application in which someone logs in and depending
on the login, a specific table for this login is used.
Storing all contacts in one table with an identifier of e.g. ash ted for
each record is not possible as from a security point, I need to keep
contacts for each login in a separate table.

So here we are again at the beginning. How can I do something like:

DECLARE
@AddressID int,
@ProgClient (varchar(10),
@Table varchar(10)

-- @Prog is e.g. 'ash'

SET @Table = 'i2b_ + @ProgClient + '_contact

SET @AddressID = (SELECT AddressID FROM @Table WHERE ContactID = @ContactID)

"David Portas" <RE************ *************** *@acm.org> wrote in message
news:V9******** ************@gi ganews.com...
I need to evaluate @Table first otherwise it can't find it.
Unless you make it just one table and put the Progclient value in as a
column. Seems like an unusual design to have multiple tables of Contact
details.

CREATE TABLE Contacts (progclient VARCHAR(10), contactid INTEGER,

addressid INTEGER NOT NULL, ... PRIMARY KEY (progclient, contactid))

SET @AddressID =
(SELECT AddressID
FROM Contacts
WHERE ContactID = @ContactID
AND progclient = @ProgClient)

--
David Portas
------------
Please reply only to the newsgroup
--

Jul 20 '05 #6
> each record is not possible as from a security point, I need to keep
contacts for each login in a separate table.


Row-level security:

http://vyaskn.tripod.com/row_level_s..._databases.htm

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #7
Thanks guys for answering my question.
I've spent some time on Erland's site and came to the conclusion that
I better follow a wise man's advice!

Have a nice day!

"David Portas" <RE************ *************** *@acm.org> wrote in message news:<nJ******* *************@g iganews.com>...
each record is not possible as from a security point, I need to keep
contacts for each login in a separate table.


Row-level security:

http://vyaskn.tripod.com/row_level_s..._databases.htm

Jul 20 '05 #8

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

Similar topics

1
392
by: Martin | last post by:
Hi! I would be grateful for any advise regarding what I'm doing wrong.. My brain is stuck. Probably some stupid simple mistake I don't see. Thanks very much for your efforts! Martin I have this code: DECLARE @ContactID varchar(10),
119
4526
by: rhat | last post by:
I heard that beta 2 now makes ASP.NET xhtml compliant. Can anyone shed some light on what this will change and it will break stuff as converting HTML to XHTML pages DO break things. see, http://www.alistapart.com/articles/betterliving/ I read on...
6
1734
by: Materialised | last post by:
Hi, could someone have a look at the following code and the error message below, and point out where my error is, as I am having trouble figuring where I am going wrong. #include <algorithm> #include <iomanip> #include <ios> #include <iostream> #include <string> #include <vector>
16
2065
by: Justin Hoffman | last post by:
This is a question concerning query optimisation. Sorry if it's a bit long, but thanks to anyone who has the patience to help - This is my first post here... If I have two tables: 'tblContact' and 'tblCategory' where categories are like: Code Name 010101 Short 010102 Fat
4
1146
by: ad | last post by:
I have 50 web page in my project, there always a DataGrid in every page. The PreRender event is the same all of the DataGrid I have copy/paste the code of PreRender many many times. I think there must be a method instead of copy/paste How can I reuese the event for all the DataGrids ?
4
1226
by: Smoothcoder | last post by:
Hi, there, smart guys! I have an AMD 64 3000+ based system, with Windows XP Professional on it; I recently installed Visual Studio 2003 Enterprise Architect; I created a new C# ASP.NET Web Application project (with only the default blank page) and immediately tried to start the project - it does not work, showing me the following stupid...
15
2408
by: sparks | last post by:
We get more and more data done in excel and then they want it imported into access. The data is just stupid....values of 1 to 5 we get a lot of 0's ok that alright but 1-jan ? we get colums that are formatted for number and then half way down they are changed to text. OR the famous ok now everything in red is ---- and everything in blue...
0
2088
by: LadyJ | last post by:
Im coding in VB.NET and XAML for a project and have a problem creeping in is that when i check my combo box to see if there is anything in it so i can display appropriate image it does not work. If CmboDJList.HasItems Then .... OR If Not CmboDJList.HasItems Then .... Comes up with the same answer... it says YES... no matter...
9
1332
by: Carl Johansson | last post by:
If a multithreaded .NET application is executed on a computer with a multicore processor. Will the application automatically run the threads on different processor cores? Regards Carl Johansson
0
7693
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...
0
7604
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...
0
7962
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...
0
6275
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...
1
5498
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...
0
5217
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...
0
3651
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...
1
2101
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
932
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...

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.