Use case statement for parameter for column name in sql where clause - asp.net

I have been looking all day for a solution that works for my situation. I have found some things that are very similar but don't work for my situation, I tried them.
Here is the scenario; I have two table base and partdetails. I have an asp website (internal ONLY) that has drop down lists to select the parameters for a SQL query that fills a data grid view.
My problem is this, I need to be able, based on the drop down list boxes on the page, assign the column name that the criteria that is entered to be searched for.
Here is the query that I am trying to define: (This one returns 0 rows)
sqlCmd.CommandText = ("Select ba.referenceid, ba.partnum, pd.width, pd.length, CONVERT(varchar(12), pd.dateentered, 101) As [dateentered], ba.partqty, ba.status, ba.material From tbl_dlbase ba Join tbl_partdetails pd On ba.referenceid = pd.referenceid Where Case #field1 When 'part #' Then 'ba.partnum' When 'Spacing' Then 'pd.spacing' When 'Surface' Then 'pd.surface' When 'Height' Then 'pd.height' When 'Thickness' Then 'pd.thickness' End Like '%' + #criteria1 + '%'")
sqlCmd.Parameters.AddWithValue("#field1", ddlSc1.SelectedItem.Text)
sqlCmd.Parameters.AddWithValue("#criteria1", txbCriteria1.Text)
This is the latest version of the SQL statement that I have tried. I need to be able to set the field/column name based on the selection from the drop down list ddlsc1 on the asp page.
I have also been trying the queries in Studio manager to see if maybe I have fat fingered something but it also returns 0 rows so I know something is wrong with the query.
So how can I set the column name field using a parameter for the name. I know this is a huge security concern with SQL injection but this is an internal only site, and more importantly my boss said he wants it done with variables.

I don't really see a problem with this other than you have single quotes around your THEN values. Does this fix it?
SELECT ba.referenceid
,ba.partnum
,pd.width
,pd.length
,CONVERT(VARCHAR(12), pd.dateentered, 101) AS [dateentered]
,ba.partqty
,ba.STATUS
,ba.material
FROM tbl_dlbase ba
JOIN tbl_partdetails pd ON ba.referenceid = pd.referenceid
WHERE CASE #field1
WHEN 'part #'
THEN ba.partnum
WHEN 'Spacing'
THEN pd.spacing
WHEN 'Surface'
THEN pd.surface
WHEN 'Height'
THEN pd.height
WHEN 'Thickness'
THEN pd.thickness
END LIKE '%' + #criteria1 + '%'

Related

Is it possible to invert the order of "LIKE" operation?

My database has a table with columns genus, species, and inatcode. inatcode is blank for every row it hasn't been manually added to. I imported a new table that contains all the codes and want to create an Update query that copies them all to the corresponding rows of the first table. However, because the species column of my first table has additional strings, the match is not perfect and many rows were not updated. table.a.species might look like :
x bimundorum
vesicula (sexgen)
sinuata breviloba
And I want it to match these values in table.b.species:
bimundorum
vesicula
sinuata
I know to use table.a.species LIKE '%table.b.species%' when b is a substring of a, but this is the opposite case and just flipping (shown below) doesn't seem to work. Is there another way to accomplish this in SQLite? The differences between a and b are heterogeneous, but there are only a few cases and I could potentially do multiple queries to account for each.
"UPDATE table.a SET inatcode = table.b.inatcode
FROM table.b
WHERE table.b.genus = table.a.genus
AND table.a.species LIKE '%table.b.species%' "
I am working with SQLite through the DBI package in R, and could make all of this happen in R and reinsert instead. But it seems like this I should be able to do in SQLite.
The modified formatting posted here does what I wanted; I just needed to use the || to concatenate the values and the % operator to make the LIKE function behave as I wanted it to.
"UPDATE table.a SET inatcode = table.b.inatcode
FROM table.b
WHERE table.b.genus = table.a.genus
AND table.a.species LIKE '%' || table.b.species || '%'
AND table.b.species != '' "

How do I limit query results, when distinct isn't distinct?

I have a bill of material file that I am trying to reduce to only the unique parts, and related data for the line. The problem I'm running into is multiple instances of a part number due to variations in the formatting or language in the part name from the system/s that a third party pulls the data from.
pn123 part_name
pn123 Part-name
pn123 Part name
pn123 German name
All other fields I select are equal, how do I limit this in the where clause to just one instance of the above for all different part numbers? Is there an equivalent to MAX() in a text string?
I am working around the issue in excel, by deleting the dupes.
select distinct
adhoc.ats_esh.Customs_Entry_Num
[VIN]as [Qlickview VIN]
,[Build_Date]
,[BOM_Level]
,[9802].[Supplier]
,[Part_number]
,[Part_Name] *******THIS IS THE PROBLEM FIELD*******
,[Unit_Price]
,[Usage]
,[Extended_Price]
from
adhoc.IMFM_9802_EU_AP [9802] inner join ADHOC.ATS_ESL
ON [9802].VIN = ADHOC.ATS_ESL.Part_Num
inner join adhoc.ATS_ESH
ON ADHOC.ATS_ESH.Trans_SK = ADHOC.ATS_ESL.Trans_SK
where
adhoc.ats_esh.importer ='ACME_CO'
and adhoc.ATS_ESH.ENTRY_SUMMARY_DATE >= '2/01/2018'
And adhoc.ATS_ESH.ENTRY_SUMMARY_DATE < '3/01/2018'
AND adhoc.ats_esl.Supplier in('supplier1','supplier2','supplier3')
--and adhoc.ats_esl.Part_Num like '%ABC%'
--and [BOM_Level] = '1' --**** use MAX()
The right way to do this is to have a table with part name and number where part number is a unique key. This way you join on Part_number and get a unique Part_name. You can also use Max(Part_name) if you use Group by Part_number but this will require you to rework your query a little bit.

SQLLite howto join mutiple tables without column ambiguity

I've the following simplified SQLLite query:
SELECT SPECIALTABLE.DETAILS AS Details
FROM SPECIALTABLE
INNER JOIN CUSTOMERTABLE
ON CUSTOMERTABLE.CUSCODE = SPECIALTABLE.CUSCODE
INNER JOIN CUSTOMERNAMETABLE
ON CUSTOMERNAMETABLE.NAMECODE = CUSTOMERTABLE.NAMECODE
WHERE NAMECODE LIKE '%' LIMIT 10
SPECIALTABLE does not contain the NAMECODE column.
But when I run the query, it gives the "ambiguous column name" error on NAMECODE. I do not want to specify the table however on namecode (e.g. WHERE CUSTOMERTABLE.NAMECODE LIKE ...).
To my understanding, SQLLite should treat the result of these joins like one big table containing all columns that are contained in each of the tables, so why is there ambiguity?
The problem is there is no column directly linking SPECIALTABLE and CUSTOMERNAMETABLE.
When you write your last line, you do not specify what NAMECODE is in condition. So, the SQL can't catch the NAMECODE correctly, because there is two definitions for them (CUSTOMERNAMETABLE.NAMECODE and CUSTOMERTABLE.NAMECODE)
WHERE NAMECODE LIKE '%' LIMIT 10
Try to Specify the Table that you want the NAMECODE condition, like this:
WHERE CUSTOMERNAMETABLE.NAMECODE LIKE '%' LIMIT 10
Or this:
WHERE CUSTOMERTABLE.NAMECODE LIKE '%' LIMIT 10

AssertResultSetsHaveSameMetaData in TSQLT

I am using TSQLT AssertResultSetsHaveSameMetaData to compare metadata between two tables.But the problem is that i cannot hardcode the table name since i am passing the table name as the parameter at the runtime.So is there any way to do that
You use tSQLt.AssertResultSetsHaveSameMetaData by passing two select statements like this:
exec tSQLt.AssertResultSetsHaveSameMetaData
'SELECT TOP 1 * FROM mySchema.ThisTable;'
, 'SELECT TOP 1 * FROM mySchema.ThatTable;';
So it should be quite easy to parameterise the names of the tables you are comparing and build the SELECT statements based on those table name parameters.
However, if you are using the latest version of tSQLt you can also now use tSQLt.AssertEqualsTableSchema to do the same thing. You would use this assertion like this:
exec tSQLt.AssertEqualsTableSchema
'mySchema.ThisTable'
, 'mySchema.ThatTable';
Once again, parameterising the tables names would be easy since they are passed to AssertEqualsTableSchema as parameters.
If you explain the use case/context and provide sample code to explain what you are trying to do you stand a better chance of getting the help you need.

select update within sama table oracle sql or pl/sql -

I need to select v_col1, from table_x and that column gives me string that i need to put(update) into same
rowid but into diffrent column(h_col2) in sama table table_x - sorry it seems easy but i am beginner....
tabl_x
rowid V_col1, h_col2 etc .....
1 672637263 GVRT1898
2 384738477 GVRT1876
3 263237863 GVRT1832
like in this example i need to put GVRT1898 (update) instead of 672637263 and i need to
go into every row in this table_x and fix -
like next line would be (rowid2 would be GVRT1876 instead of 384738477 :-)
this table has 40000 lines like this and i need to loop for every rowid
THX for your responce Justin - this is a little more complex,
i have this string in h_col and need to take only GVRTnumber out and put into v_col - but it's
hard becouse GVRTnumber is in various place in column see down here....
"E_ID"=X:"GVRT1878","RCode"=X:"156000","Month"=d:1,"Activate"=d:5,"Disp_Id"=X:"4673498","Tar"=X:"171758021";
2"E_ID"=X:"561001760","RCode"=X:"156000","Month"=d:1,"Activate"=d:5,"Disp_Id"=X:"GVRT1898","Tar"=X:"171758021";
h_col column have this number that i want but in various place like somethimes it's in this 600byte column it's in byte nr 156 - sometimes in 287 but the only unique is "GVRT...." how can i take that string and put it to v_col -
Can you show me how to write such SQL pl/sql ?
regards & thanks
It sounds like you just want
UPDATE tabl_x
SET h_col2 = v_col1
Of course, if you do something like this, that implies that one of the two columns should be dropped or the data model needs to get fixed. Having two copies of the same data in each row is a bad idea from a normalization standpoint if nothing else.

Resources