473,408 Members | 2,427 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,408 software developers and data experts.

How to use where wiht variable

3
Hi,
i have a "little" problemm. I need to use local variable, but complete condition is in one of sql tables.

For example, i wil write this way:

DECLARE @STRING VARCHAR(30)

SELECT @STRING = (SELECT MYCONDITION FROM CONDITIONTABLE WHERE CONDITION = 1)

-- HERE I HAVE A ANSWARE FROM SQLSERVER
-- @STRING = 'YEAR = 2009'

SELECT *
FROM MYDATATABLE
WHERE @STRING


Any ideas? :)

Tnx, Dat
Sep 8 '09 #1
5 2061
ck9663
2,878 Expert 2GB
Depending on your need, you might be able to use a dynamic query or a table-function.

Happy Coding!!!

--- CK
Sep 10 '09 #2
topci
3
Oh, sory, but i need more information. It's be very helpfull whenn cann you give me some example... In moment i will use a "build" query with exec (query), but if have someone some better idea, i will be "old and happy" :) ...
Sep 10 '09 #3
ck9663
2,878 Expert 2GB
Could you explain your requirement a little bit more detail? Are you saying the column on your WHERE condition could vary? Or the value of the variable on the WHERE condition could vary but the columns are fixed?

--- CK
Sep 11 '09 #4
topci
3
Hmmm... I will explane the problem with "psevdo" code (sorry for my Englesh)...

I have sales data in table KUM_SALES (like cumulative_sales). Condition is written in table SALES_COND.
Kum_sales have fields like:
- YEAR
- CUSTOMER_ID
- QUANTITY
- VALUES_ WITHOUT_VAT
- VAT
- VALUES_WITH_VAT

Table SALES_COND have fields:
- TYPE_COND (tipe of condition)
- COND (description of condition)

for exemple, my customer will choice:

TYPE_COND = 1 OR 2 OR 3, ...

IN TYPE OF CONDITION IS WRITEN:

1 = YEAR = 2009 AND CUSTOMER_ID IN ('123','223','333')
2 = YEAR = 2008 AND CUSTOMER_ID IS NOT IN ('123','223','333')
3 = CUSTOMER_ID LIKE '5%'
...

the main quiry is the same for every type of condition:

SELECT YEAR,
CUSTOMER_ID,
SUM(QUANTITY) AS QUIANTITY,
SUM(VAT) AS VAT,
...
FROM KUM_SALES
WHERE ...
-- here need to be (select from a cond_table where type_cond = for example 3)
CUSTOMER_ID LIKE '5%'


I think it cann be solved with SP_EXCECUTEQUIRY, where i cann build quiry... But whenn is posible to geth a simples example, it will be very usefull

Tnx,
Darko
Sep 12 '09 #5
ck9663
2,878 Expert 2GB
When you said your customer chooses a condition, are you talking about some kind of GUI/Application that has a drop-down or something like that, that they can use to choose the condition? If YES, then you can build your query dynamically on your front-end.

Happy Coding!!!


--- CK
Sep 12 '09 #6

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

Similar topics

1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
5
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
1
by: Burton Wilkins | last post by:
Dear Brandon: You sent me some advice before as to how get the DataLink working, but I haven't been able to get your solution to compile: You sent me this: >Hi Burton, I was able to get the...
4
tolkienarda
by: tolkienarda | last post by:
Hi all I work for a small webdesign company and we have remote hosting. i built a mysql database with phpmyadmin on the server. i then downloaded and modified a php login page. i am continuing to...
10
by: Maria Mela | last post by:
Hello Everyone, What´s wrong in my code?? I can´t compile and sort my struct data... Here´s peace of my code... typedef struct student { int num_stu;
3
by: Krish | last post by:
I want to have an enum that has a list of numeric values. How do I achieve this? Something like this ... Public Enum AllowedNumbers { 111, 222, 333,
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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,...
0
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...

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.