简体   繁体   中英

Pass parameter value to sql query

Simple question - have below query:

SELECT 
    CASE 
      WHEN r.number Is NULL THEN 1001
      WHEN r.number = 9999 THEN 10001
      ELSE r.number + 1 
    END number
FROM 
    (VALUES(123)) AS a(art)
LEFT JOIN
    (SELECT 
         PARSENAME(Nummer, 3) art,
         MAX(CAST(PARSENAME(Nummer, 2) AS INT)) number
     FROM 
         tbArtikel 
     WHERE 
         Nummer LIKE '123[.]%'
     GROUP BY 
         PARSENAME(Nummer, 3)) r ON r.art = a.art

I want to pass value from parameter to it in two places - where you see 123. I tried do like this but seems something is wrong, can you tell me how this should be done?

That's how I did it (wrong):

Using cmd As New SqlCommand("SELECT CASE WHEN r.number Is NULL THEN 1001
                                                     WHEN r.number = 9999 THEN 10001
                                                     Else r.number + 1 End number
                                        FROM (VALUES(@producentNumber)) AS a(art)   -- this will search this number within inner query And make case..
                                        LEFT JOIN(
                                        -- Get producent (in Like) number And max number Of it (without Like it Get all producent numbers And their max number out Of all
                                        SELECT PARSENAME(Nummer, 3) art,
                                        MAX(CAST(PARSENAME(Nummer, 2) AS INT)) number
                                        FROM tbArtikel WHERE Nummer Like '@producentNumber[.]%'
                                        GROUP BY PARSENAME(Nummer, 3)
                                        ) r
                                        On r.art = a.art", con)

cmd.CommandType = CommandType.Text
cmd.Parameters.Add("@producentNumber", SqlDbType.Int)

You have missing quots here

Like ' + @producentNumber + '[.]%'

Try this:

  Using cmd As New SqlCommand("SELECT CASE WHEN r.number Is NULL THEN 1001
                                                     WHEN r.number = 9999 THEN 10001
                                                     Else r.number + 1 End number
                                        FROM (VALUES(@producentNumber)) AS a(art)   -- this will search this number within inner query And make case..
                                        LEFT JOIN(
                                        -- Get producent (in Like) number And max number Of it (without Like it Get all producent numbers And their max number out Of all
                                        SELECT PARSENAME(Nummer, 3) art,
                                        MAX(CAST(PARSENAME(Nummer, 2) AS INT)) number
                                        FROM tbArtikel WHERE Nummer Like ' + @producentNumber + '[.]%'
                                        GROUP BY PARSENAME(Nummer, 3)
                                        ) r
                                        On r.art = a.art", con)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.Add("@producentNumber", SqlDbType.Int)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM