473,396 Members | 1,743 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.

Help With Query

I have a table as below
---------------------------
name linkcode level
---------------------------
brando 1,3,8 1
damroo 1,5 2
rogers 2,7 1
shane 1,7 1
Now I want a query where I pass a level and linkcode (one of the codes
in Comma Seperated values in the record) and it returns me a recordset
with matching criteria like below

I have a variable called lcode (int) and a variable called ulevel (int)
if I pass lcode = 7 and level = 1 it should return rogers, shane in the
record set
My sql is as below

"Select name from tblnames where level =" & ulevel & " and " & lcode &
" in (linkcode)"

I am unable to run this query as it returns an error that
Syntax error converting the varchar value '1,3,8' to a column of data
type int

I even tried by updating the query with a UDF Split function, which
works fine for a specific string CSV as params but does not take the
CSV as a Fieldvalue
"Select name from tblnames where level =" & ulevel & " and " & lcode &
" in (select value from dbo.split(linkcode))"

returns
linkcode is not a recognized OPTIMIZER LOCK HINTS option

can anyone help with this.. or suggest an alternative SQL for my
problem..
I am using MS Sql Server 2000

thanks

Aug 22 '05 #1
3 4937
Damroo wrote:
I have a table as below
---------------------------
name linkcode level
---------------------------
brando 1,3,8 1
damroo 1,5 2
rogers 2,7 1
shane 1,7 1
Now I want a query where I pass a level and linkcode (one of the codes
in Comma Seperated values in the record) and it returns me a recordset
with matching criteria like below

I have a variable called lcode (int) and a variable called ulevel (int)
if I pass lcode = 7 and level = 1 it should return rogers, shane in the
record set
My sql is as below

"Select name from tblnames where level =" & ulevel & " and " & lcode &
" in (linkcode)"

I am unable to run this query as it returns an error that
Syntax error converting the varchar value '1,3,8' to a column of data
type int

I even tried by updating the query with a UDF Split function, which
works fine for a specific string CSV as params but does not take the
CSV as a Fieldvalue
"Select name from tblnames where level =" & ulevel & " and " & lcode &
" in (select value from dbo.split(linkcode))"

returns
linkcode is not a recognized OPTIMIZER LOCK HINTS option

can anyone help with this.. or suggest an alternative SQL for my
problem..
I am using MS Sql Server 2000

thanks


If your linkcode is truly holding multiple separate values what you have
here, by definition, is a bad design: Fix the design.

Trying to find a work-around is the same as trying trying to sing "la la
la" really loud to drown out the sound of someone telling you your house
is on fire.
--
Daniel A. Morgan
http://www.psoug.org
da******@x.washington.edu
(replace x with u to respond)
Aug 22 '05 #2
This might work (another version of your first query):

"Select name from tblnames where level = " & ulevel & " and linkcode
like '%" & lcode & "%'"

However, in general storing a list in a single column indicates a
design problem, so you might want to look again at your implementation.
It's hard to be more specific without more details, though, and in
particular what "linkcode" represents - an ordered list, an unordered
list, a path in a hierarchy etc. If you can give some more information,
along with CREATE TABLE and INSERTs to show a sample case, someone may
be able to suggest an alternative approach.

This article has more information about handling delimited lists:

http://www.sommarskog.se/arrays-in-sql.html

Specifically, these comments seem to apply to your current situation:

http://www.sommarskog.se/arrays-in-s...#unpack-tblcol

Simon

Aug 22 '05 #3
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. By definition, this is not a table!! If you were
actually writing SQL and using Normal Forms would it look like this?

CREATE TABLE Foobar
(user_name CHAR(15) NOT NULL,
link_code INTEGER NOT NULL,
PRIMARY KEY (user_name,link_code),
foo_level INTEGER NOT NULL);

INSERT INTO Foobar VALUES ('brando', 1, 1);
INSERT INTO Foobar VALUES ('brando', 3, 1);
INSERT INTO Foobar VALUES ('brando', 8, 1);
INSERT INTO Foobar VALUES ('damroo', 1, 2);
INSERT INTO Foobar VALUES ('damroo', 5, 2);
INSERT INTO Foobar VALUES ('rogers', 2, 1);
INSERT INTO Foobar VALUES ('rogers', 7, 1);
INSERT INTO Foobar VALUES ('shane', 1, 1);
INSERT INTO Foobar VALUES ('shane', 7, 1);
a query where I pass a level and linkcode (one of the codes in Comma Seperated values in the record [sic]) and it returns me a recordset with matching criteria like below <<


Rows are not anything like records. Columns are supposed to be
scalars! No wonder you do not understand Normal Forms!!

SELECT user_name
FROM Foobar
WHERE foo_level = @my_lvl
AND link_code = @my_link;

See how easy it is when you follow the basics. Please read a book on
RDBMS before you code or post anything again.

Aug 22 '05 #4

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

Similar topics

11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
5
by: Steve Patrick | last post by:
Hi All You guys are my last hope, despite spending money on books and hours reading them I still can not achieve the results I need. I have designed a database in Access 2000 based on 1 table,...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
3
by: mcmahonb | last post by:
Hey people... I've been searching this forum for a few hours and even though this topic has been went over from many different angles; I cannot seem to figure out how to make things work on my...
4
by: n | last post by:
Hello! Here is a problem I hope you can point me to a solution. It Problem: A teacher needs to know which lesson to teach. A school has a curriculum with 26 lessons, A-Z. For a given class,...
47
by: Jo | last post by:
Hi there, I'm Jo and it's the first time I've posted here. I'm in process of creating a database at work and have come a little unstuck.....I'm a bit of a novice and wondered if anyone could...
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: 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
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...
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,...

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.