Solved

SQL search query

Posted on 2013-01-04
5
344 Views
Last Modified: 2013-01-04
I have a sql query - somewhere in here I have to add a search feature - i have a text box I call txtSearch - i want to pass this string value into this and have it return those results from this query can anyone help please

SELECT DISTINCT qci.id 
                AS 
                qcuid, 
                t.uid 
                AS taskuid, 
                t.polinekey, 
                cf.factoryuid, 
                Cast(Cast(ponumber AS INT) AS VARCHAR) 
                AS ponumber, 
                Upper(Isnull(custid, '')) 
                AS custid, 
                Upper(COALESCE(NULLIF(custname, ''), shiptoname)) 
                AS custname, 
                Isnull(t.selectedtask, 1) 
                AS selectedtask, 
                CASE 
                  WHEN t.qctestsampleuid > 0 THEN (SELECT 
                  Upper(COALESCE(masitemno, uniqueid)) 
                                                   FROM   ideas 
                                                   WHERE 
                  ideauid = (SELECT ideauid 
                             FROM   task 
                             WHERE  uid = st.parentuid)) 
                  ELSE Upper(itemid) 
                END 
                AS itemid, 
                CASE 
                  WHEN t.qctestsampleuid > 0 THEN (SELECT Upper(itemdesc) 
                                                   FROM   ideas 
                                                   WHERE 
                  ideauid = (SELECT ideauid 
                             FROM   task 
                             WHERE  uid = st.parentuid)) 
                  ELSE Upper(vpo.description) 
                END 
                AS itemdescription, 
                Cast(COALESCE(rh.taskuid, 0) AS BIT) 
                AS Uploaded, 
                dbo.Kkgetallowsubmitbypass(t.uid) 
                AS enableBypass, 
                Cast(COALESCE(t.resultsid, 0) AS BIT) 
                AS uploadcompeted, 
                t.resultsid, 
                CASE 
                  WHEN t.qctestsampleuid > 0 THEN Upper( 
                  COALESCE (st.designer, '')) 
                  ELSE Upper(factory) 
                END 
                AS factory, 
                companyid, 
                NULLIF (promisedate, '01/01/1900') 
                AS POShipdate, 
                vpo.polinekey, 
                vpo.pokey, 
                purchamt, 
                qtyleft, 
                sumqtytoload, 
                sumqtyloaded, 
                qtyord, 
                qtyopentorcv, 
                companyid, 
                qci.id 
                AS qcInspectionUID, 
                qci.customeritem, 
                qci.customerpo, 
                qci.custbatchdatecode, 
                CASE Rtrim(Ltrim(qci.batchcode)) 
                  WHEN '' THEN Cast (Cast(vpo.ponumber AS INT) AS VARCHAR) 
                               + '/' + vpo.itemid + '/' 
                               + Cast (Datepart(wk, vpo.promisedate) AS VARCHAR)
                   ELSE Rtrim(Ltrim(qci.batchcode)) 
                END 
                AS batchcode, 
                qci.label_needsilversticker, 
                qci.label_needbranding, 
                qci.label_needcarbsticker, 
                qci.label_needfabriclabel, 
                qci.label_needenginewarning, 
                t.designer 
                AS Inspectorname, 
                t.comments 
                AS inspectorinstructions, 
                t.designeruid 
                AS inspectorUID, 
                t.daterequested, 
                NULLIF (t.datetobecomplete, '01/01/1900') 
                AS inspectiondate, 
dbo.Kktaskdisablewithoutsignoff(t.polinekey, t.parentuid, t.datetobecomplete) AS
 TaskDisableWithoutSignOff, 
qci.label_needflyer, 
NULLIF (t.completeddate, '01/01/1900')                                        AS
                 completeddate, 
NULLIF (t.downloaddate, '01/01/1900')                                         AS
                 DownloadDate, 
t.successfuldownload, 
t.isxmlreleased, 
Isnull(uc.elevateflag, 0)                                                     AS
                 elevateflag, 
Isnull(uc.commentuid, 0)                                                      AS
                 commentuid, 
Isnull((SELECT 1 
        FROM   task 
        WHERE  uid = ANY (SELECT TOP 1 uid 
                          FROM   task (nolock) t2 
                                 INNER JOIN historytaskstatus AS hts (nolock) 
                                         ON hts.taskuid = t2.uid 
                                 INNER JOIN designertaskstatus dts (nolock) 
                                         ON dts.designerstatusuid = 
                                            hts.taskstatusuid 
                          WHERE  t2.uid = t.uid 
                                 AND closetask <> 0 
                                 AND wascloned = 0)), 0)                      AS
                 cloneneeded, 
Cast(CASE 
       WHEN qci.xpsreport IS NULL THEN 0 
       ELSE 1 
     END AS BIT)                                                              AS
                 uploadcompeted 
FROM   task (nolock) t 
       INNER JOIN qcinspection (nolock) qci 
               ON qci.taskuid = t.uid 
       LEFT JOIN qcfinalinspection (nolock) qfi 
              ON qfi.qcinspectionid = qci.id 
       LEFT JOIN vpoalldetailswithcust (nolock) vpo 
              ON t.polinekey = vpo.polinekey 
       LEFT JOIN sampledetail (nolock)sd 
              ON sd.sampleuid = t.qctestsampleuid 
       LEFT JOIN task (nolock)st 
              ON st.uid = sd.taskuid 
       LEFT JOIN pofactory (nolock) pof 
              ON vpo.polinekey = pof.polinekey 
       LEFT JOIN factoryinfo (nolock) cf 
              ON pof.factoryuid = cf.factoryuid 
       LEFT JOIN vfactorymgr (nolock) fm 
              ON cf.factoryuid = fm.factoryuid 
       LEFT JOIN [resultshistory] rh 
              ON rh.taskuid = t.uid 
                 AND rh.notes = 'Inspection uploaded' 
       LEFT JOIN usercomments (nolock) AS uc 
              ON uc.commentuid = (SELECT TOP 1 commentuid 
                                  FROM   usercomments 
                                  WHERE  usercomments.typeuid = t.uid 
                                         AND type = 35 
                                         AND Isnull(usercomments.commentuid, 0) 
                                             <> 0 
                                         AND ( private = 0 
                                                OR ( private <> 0 
                                                     AND userid = 406 ) ) 
                                         AND Isnull(activecmt, 0) > 0 
                                  ORDER  BY elevateflag DESC) 
WHERE  Isnull(t.uid, 0) > 0 
       AND t.tasktype = 35 
       AND ( ( Isdate(t.completeddate) = 1 
               AND ( Datediff(dd, '01/01/1900', t.datetobecomplete) > 0 ) ) 
              OR ( ( Isdate(t.completeddate) = 0 
                      OR ( Datediff(dd, '01/01/1900', t.datetobecomplete) = 0 ) 
                   ) 
                   AND ( vpo.polstatus = 2 
                          OR vpo.postatus IN ( 2, 3, 4 ) 
                          OR t.qctestsampleuid > 0 ) ) ) 

Open in new window

0
Comment
Question by:r3nder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 38744725
just add the field at the end of your select staement

........
WHERE  Isnull(t.uid, 0) > 0
       AND t.tasktype = 35
       AND ( ( Isdate(t.completeddate) = 1
               AND ( Datediff(dd, '01/01/1900', t.datetobecomplete) > 0 ) )
              OR ( ( Isdate(t.completeddate) = 0
                      OR ( Datediff(dd, '01/01/1900', t.datetobecomplete) = 0 )
                   )
                   AND ( vpo.polstatus = 2
                          OR vpo.postatus IN ( 2, 3, 4 )
                          OR t.qctestsampleuid > 0 ) ) )
AND YourSearchField = YourSearchText
0
 
LVL 13

Expert Comment

by:Ryan
ID: 38744729
What would you be expected to put into this textbox? Would it always be applied to the same field to filter on?

For example, would it always be ponumber where the user just enters a PO number?

Is there any concern about SQL injection?

Although, not the most efficient, the easiest way given that this is a large query would be to populate a DataTable with this query. Create a DataView with a source of the datatable and setting your output control's ControlSource as the DV. Then use the Filter property of the DV to apply the filter, and the control (I assume a DataGridView) will automatically be updated.
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 38744732
if you have the select statement in a string variable, it would be of the sort

SQLText = SQLText + " AND YourSearchField = '" + txtSearch.ToString() + '"
0
 
LVL 6

Author Comment

by:r3nder
ID: 38744743
SQL injection is not an issue - this is all internal - it will always be a string and I will be searching all of the fields
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 38744759
Thank you ewangoya! worked like a champ
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question