views:

54

answers:

2

I have a query like this:

SELECT TaFellesVaktjournal.*, TaBygg.ByggNavn
FROM TaFellesVaktjournal LEFT JOIN TaBygg ON
TaFellesVaktjournal.VaktFellesByggNavnId =
TaBygg.ByggBudsjtilstEiendom WHERE VaktjFellesDato
>= @dtfr AND VaktjFellesDato <= @dttl AND (VaktFellesSak
= @sok OR VaktjFellesHendelse = @sok) ORDER BY
VaktjFellesDato DESC, VaktjFellesTid DESC

However, I get an error stating that No value given for one or more required parameters.. I've specified values for @dtfr, @dttl and @sok. Also, the error goes away if I remove the parts containing @sok (everything after @dttl) to the ORDER BY.

I've made shure I specify values for sok, and that it's not empty. I've even tried specifing 2 parameters named sok (since they are two places). Anyone know how I can do this?

The code I use to create parameters is as following:

Dim conn, cmd, param
set cmd = server.createobject("adodb.command")
set param = server.createobject("adodb.parameter")
set param = cmd.createparameter("@sok", adVarChar, adParamInput, 255, sok)
cmd.parameters.append param
A: 

You can try populating the Parameter collection using a refresh command.

Set cmd.ActiveConnection = oConn
oCmd.CommandType=adCmdStoredProc
oCmd.CommandText="ap_Your_spName"
oCmd.Parameters.Refresh
oCmd.Parameters("@dtfr") = dtfr
oCmd.Parameters("@dttl") = dttl
oCmd.Parameters("@sok") = sok
oCmd.Execute

Will incur a round trip to the server, but modern computers should cope with it!

Simmo
First of all it's not a stored proc, so will this work? And second of all, what does oCmd.Parameters.Refresh do exactly? Do it look in the query for the parameters needed? And third, if it does, can I get that list printed somehow?
Alxandr
How are you calling the SQL?The refresh makes a call to the SQL Server database, and enumerates all the parameters required by the stored procedure.
Simmo
A: 

it will work for "normal" sql statements similiar like it works for storedprocedures. you have to set the CommandType property of the command object to adCmdText for "normal" sql statements AND you have to you use q questionmark "?" instead of parameter names.

you do not need the refresh method ado will do that automatically for you. (it is only useful for stored procedures either)

dim sql
dim cmd : set cmd = server.createObject("ADODB.Command")
dim param, rs

sql = "SELECT TaFellesVaktjournal.*, TaBygg.ByggNavn" &_
      " FROM TaFellesVaktjournal" &_
      " LEFT JOIN TaBygg ON TaFellesVaktjournal.VaktFellesByggNavnId = TaBygg.ByggBudsjtilstEiendom" &_
      " WHERE VaktjFellesDato >= ?" &_
      " AND VaktjFellesDato <= ?" &_
      " AND (VaktFellesSak= ? OR VaktjFellesHendelse = ?)" &_
      " ORDER BY VaktjFellesDato DESC, VaktjFellesTid DESC"

cmd.ActiveConnection = conn
cmd.CommandType = adCmdText

set param = cmd.CreateParameter("dtfr", adVarChar, , 255, dtfr)
cmd.Parameters.Append param

set param = cmd.CreateParameter("dtfr2", adVarChar, , 255, dtfr)
cmd.Parameters.Append param

set param = cmd.CreateParameter("dttl", adVarChar, , 255, dttl)
cmd.Parameters.Append param

set param = cmd.CreateParameter("sok", adVarChar, , 255, sok)
cmd.Parameters.Append param

cmd.CommandText = sql

set rs = cmd.execute

set param = nothing
set cmd = nothing
ulluoink