I have 2 tables
table1 contains data on a product, and has a column2 with a list of
values separated by a comma (ie. 1577,256,436). column2 is of type
varchar
table2 contains a column1 which has a list of ids (which are the ids
referenced in table1.column2) and another column with descriptions for
those ids. column1 is of type int
if i run a sql statement with a where clause
WHERE table2.column1 in (table1.column2)
i get an error saying its unable to convert varchar to int
the clause with the data in it should look like
WHERE table2.column1 in (1577,256,436)
is it not possible to pass a list of values from another table and
check against it, as if it was a haldfull of int values separated by a
comma?
ive also tried casting both columns to type varchar, and
table1.column2 adding single quotes using a replace() command so that
the values are varchar values, but end up with 0 results.
at the same time if i just run
WHERE table2.column1 in (1577,256,436)
meaning the data is not being passed im just typing it in, than the
query is ran just fine and produces the results im looking for.
any help on this is appreciatedYou didn't provide table definition scripts nor sample data, but I am
guessing that you will need dynamic sql here.
create table #tmp1 (id int identity, a char(1))
insert #tmp1 values ('a')
insert #tmp1 values ('2')
insert #tmp1 values ('d')
create table #tmp2 (b varchar(10))
insert #tmp2 values ('1,2')
declare @.sql varchar(100)
select @.sql = 'select * from #tmp1 where id in (' + b + ')'
from #tmp2
print @.sql
exec (@.sql)
drop table #tmp1
drop table #tmp2
TheSQLGuru
President
Indicium Resources, Inc.
<igornik@.gmail.com> wrote in message
news:1187194472.590943.165600@.d55g2000hsg.googlegroups.com...
>I have 2 tables
> table1 contains data on a product, and has a column2 with a list of
> values separated by a comma (ie. 1577,256,436). column2 is of type
> varchar
> table2 contains a column1 which has a list of ids (which are the ids
> referenced in table1.column2) and another column with descriptions for
> those ids. column1 is of type int
> if i run a sql statement with a where clause
> WHERE table2.column1 in (table1.column2)
> i get an error saying its unable to convert varchar to int
> the clause with the data in it should look like
> WHERE table2.column1 in (1577,256,436)
> is it not possible to pass a list of values from another table and
> check against it, as if it was a haldfull of int values separated by a
> comma?
> ive also tried casting both columns to type varchar, and
> table1.column2 adding single quotes using a replace() command so that
> the values are varchar values, but end up with 0 results.
> at the same time if i just run
> WHERE table2.column1 in (1577,256,436)
> meaning the data is not being passed im just typing it in, than the
> query is ran just fine and produces the results im looking for.
> any help on this is appreciated
>|||/*
Value lists are very bad design and do not belong in Relational
Databases.
I suspect that you currently have something that looks something like
this:
*/
create table #t1 (t1_id int)
create table #t2 (t2_id int, t1_ids varchar(16))
insert into #t1 (t1_id) values (1)
insert into #t1 (t1_id) values (2)
insert into #t1 (t1_id) values (3)
insert into #t1 (t1_id) values (4)
insert into #t2 (t2_id, t1_ids) values (1, '1,3,4')
insert into #t2 (t2_id, t1_ids) values (2, '1,2,3')
select * from #t1
select * from #t2
drop table #t2
drop table #t1
/*
I suspect you would try something like:
select
t1_id,
t2_id
from
t1,t2
where
t1_id in (t1_ids)
But that won't work, this value list should be stored in the proper
case below:
*/
create table #t1 (t1_id int)
create table #t2 (t2_id int, t1_id int)
insert into #t1 (t1_id) values (1)
insert into #t1 (t1_id) values (2)
insert into #t1 (t1_id) values (3)
insert into #t1 (t1_id) values (4)
insert into #t2 (t2_id, t1_id) values (1, 1)
insert into #t2 (t2_id, t1_id) values (1, 3)
insert into #t2 (t2_id, t1_id) values (1, 4)
insert into #t2 (t2_id, t1_ids) values (2, 1)
insert into #t2 (t2_id, t1_ids) values (2, 2)
insert into #t2 (t2_id, t1_ids) values (2, 3)
select * from #t1
select * from #t2
select
t1_id,
t2_id
from
#t1 t1
inner join #t2 t2 on t1.t1_id = t2.t1_id
drop table #t2
drop table #t1
On Aug 15, 12:14 pm, igor...@.gmail.com wrote:
> I have 2 tables
> table1 contains data on a product, and has a column2 with a list of
> values separated by a comma (ie. 1577,256,436). column2 is of type
> varchar
> table2 contains a column1 which has a list of ids (which are the ids
> referenced in table1.column2) and another column with descriptions for
> those ids. column1 is of type int
> if i run a sql statement with a where clause
> WHERE table2.column1 in (table1.column2)
> i get an error saying its unable to convert varchar to int
> the clause with the data in it should look like
> WHERE table2.column1 in (1577,256,436)
> is it not possible to pass a list of values from another table and
> check against it, as if it was a haldfull of int values separated by a
> comma?
> ive also tried casting both columns to type varchar, and
> table1.column2 adding single quotes using a replace() command so that
> the values are varchar values, but end up with 0 results.
> at the same time if i just run
> WHERE table2.column1 in (1577,256,436)
> meaning the data is not being passed im just typing it in, than the
> query is ran just fine and produces the results im looking for.
> any help on this is appreciated|||thanks for all of your help, this got me going into the right
direction again.
Showing posts with label table1. Show all posts
Showing posts with label table1. Show all posts
Friday, March 30, 2012
Monday, March 12, 2012
Problem need help
ok the criteria with hath i have the problem looks like this
SELECT DISTINCT table1.col1,...,table1.columnN,.....TableM. Column X
FROM Tables1, Table M
INNER JOIN Table1
ON (table1.col1 = tableN.colZ) , Tablex
WHERE (table1.col1 = tableN.colZ)
AND (table1.col1 LIKE '.........')
:)
in this section '......' i have to introduce the next statment editbox text or other component externaly introduced datavalue wich is al integer value asstring
:eek:yess it is exactly whath i want to use an earlier or exactly the last data introduced in the table to wiew all the modification iff necesary, and to bee able to list somme modified data if it is needed.
thanks bekause sommebody sad to mee thath i have to write somme special procedure to do this thing.|||yess the ideea is good, but it gives me back somme empty cells and i'm sure thath i have data in the tables|||the code is the folowing:
"SELECT DISTINCT Contractdetailat.NRCONTRACT, Contractdetailat.DINDATA,
Contractdetailat.PINALADATA, Contractdetailat.PUNCTDELUC,
Contractdetailat.COMPONENTE, Contractdetailat.PRETUNITAR,
Contractdetailat.NRBUC, Contractdetailat.PRETTOTAL,
Firma.DENUMIREFI, Firma.NRREGCOM, Firma.CODFISCAL,
Firma.SEDIULSOCI, Firma.JUDETUL, Firma.CODIBAN,
Firma.BANCA, Firma.PUNCTDELUC, Firma.NRCONTRACT,
Delegati.NUMELEDELE, Delegati.BI_CI_, Delegati.SERIA,
Delegati.ELIBERATPO, Delegati.MIJLOCTRAN,
Delegati.NRMASINA, Delegati.DATAEXPED
FROM "D:\ISTI\28.11.2006\contractdetailat.dbf" Contractdetailat
INNER JOIN "D:\ISTI\28.11.2006\firma.dbf" Firma
ON (Contractdetailat.NRCONTRACT = Firma.NRCONTRACT) , "D:\ISTI\28.11.2006\delegati.dbf" Delegati
WHERE (Contractdetailat.NRCONTRACT = Firma.NRCONTRACT)
AND (Contractdetailat.NRCONTRACT like'?"&intrari.nrcontract.text&"*')"
now the rezult soud be
at nrconract i have 25 contracts with the afferent next rows and i don't need all of it listed in the same sheet in one sheet i need listig of only one contract that is tha ideea.
thanks.
by the way the program is one man creation i'm vorking solo and it is werry hard to work solo|||the return rezult is zipp !!!!!
it is a blank datatable
I'm sure there is somme data on it because it when i select all it can be displayd in a data gird
SELECT DISTINCT table1.col1,...,table1.columnN,.....TableM. Column X
FROM Tables1, Table M
INNER JOIN Table1
ON (table1.col1 = tableN.colZ) , Tablex
WHERE (table1.col1 = tableN.colZ)
AND (table1.col1 LIKE '.........')
:)
in this section '......' i have to introduce the next statment editbox text or other component externaly introduced datavalue wich is al integer value asstring
:eek:yess it is exactly whath i want to use an earlier or exactly the last data introduced in the table to wiew all the modification iff necesary, and to bee able to list somme modified data if it is needed.
thanks bekause sommebody sad to mee thath i have to write somme special procedure to do this thing.|||yess the ideea is good, but it gives me back somme empty cells and i'm sure thath i have data in the tables|||the code is the folowing:
"SELECT DISTINCT Contractdetailat.NRCONTRACT, Contractdetailat.DINDATA,
Contractdetailat.PINALADATA, Contractdetailat.PUNCTDELUC,
Contractdetailat.COMPONENTE, Contractdetailat.PRETUNITAR,
Contractdetailat.NRBUC, Contractdetailat.PRETTOTAL,
Firma.DENUMIREFI, Firma.NRREGCOM, Firma.CODFISCAL,
Firma.SEDIULSOCI, Firma.JUDETUL, Firma.CODIBAN,
Firma.BANCA, Firma.PUNCTDELUC, Firma.NRCONTRACT,
Delegati.NUMELEDELE, Delegati.BI_CI_, Delegati.SERIA,
Delegati.ELIBERATPO, Delegati.MIJLOCTRAN,
Delegati.NRMASINA, Delegati.DATAEXPED
FROM "D:\ISTI\28.11.2006\contractdetailat.dbf" Contractdetailat
INNER JOIN "D:\ISTI\28.11.2006\firma.dbf" Firma
ON (Contractdetailat.NRCONTRACT = Firma.NRCONTRACT) , "D:\ISTI\28.11.2006\delegati.dbf" Delegati
WHERE (Contractdetailat.NRCONTRACT = Firma.NRCONTRACT)
AND (Contractdetailat.NRCONTRACT like'?"&intrari.nrcontract.text&"*')"
now the rezult soud be
at nrconract i have 25 contracts with the afferent next rows and i don't need all of it listed in the same sheet in one sheet i need listig of only one contract that is tha ideea.
thanks.
by the way the program is one man creation i'm vorking solo and it is werry hard to work solo|||the return rezult is zipp !!!!!
it is a blank datatable
I'm sure there is somme data on it because it when i select all it can be displayd in a data gird
Subscribe to:
Posts (Atom)