SQL Server error 80040e14 Using Classic ASP in a Result Set - sql-server

I'm trying to select results from a database for each position of a vector using classic ASP and SQL Server. The code so far:
FOR EACH x IN Tabela
sql = "SELECT DISTINCT tborders.family AS family, tborders.qty AS qty, tborders.los AS los, CONVERT(DATE, tborders.mrd_date) AS mrd FROM [DASH].[dashboard_db].[dbo].[tb_family] AS tbfamily INNER JOIN [DASH].[dashboard_db].[dbo].[tb_started_zero] AS tborders ON tbfamily.[family] = tborders.[family] WHERE tborders.[Order Number] = "&x&""
SET rs = conn.execute(sql)
IF rs.EOF = false THEN
mrd(counter) = rs("mrd")
family(counter) = rs("family")
los(counter) = rs("los")
qty(counter) = rs("qty")
counter=counter+1
END IF
NEXT
Taking note that tborders.[Order Number] is a int typed value. For some reason I'm having this error:
Microsoft SQL Server Native Client 11.0 error '80040e14'
Incorrect syntax near '='.
/asplearning/act/validate-schedule-line.asp, line 46
I have tried removing the SET, but then my result set isn't recognized as a object. I'm pretty sure that the types are just fine, I have tried:
if isNumeric(x) THEN
response.write("<h1>it is numeric</h1>")
else
response.write("<h1>not numeric</h1>")
end if
And it has written "it is numeric" for each position of Tabela. Can anyone help with what it seems to be the problem?

It's look you have an empty item (first or last item of the collection).
I strongly suggest you to use sp_executesql. This will use a compiled execution plan, and this will validate your parameters (against sql injection).
FOR EACH x IN Tabela
if len(x) > 0 then
sql = "exec sp_executeSql N'SELECT DISTINCT tborders.family AS family, tborders.qty AS qty, tborders.los AS los, CONVERT(DATE, tborders.mrd_date) AS mrd FROM [DASH].[dashboard_db].[dbo].[tb_family] AS tbfamily INNER JOIN [DASH].[dashboard_db].[dbo].[tb_started_zero] AS tborders ON tbfamily.[family] = tborders.[family] WHERE tborders.[Order Number] = #OrderNumber', N'#OrderNumber int', #orderNumber = " & x
SET rs = conn.execute(sql)
IF rs.EOF = false THEN
mrd(counter) = rs("mrd")
family(counter) = rs("family")
los(counter) = rs("los")
qty(counter) = rs("qty")
counter=counter+1
END IF
rs.close
end if
NEXT

Related

How to update multiple records in SQL using Classic ASP

I'm trying to update a two records with the same social in Classic ASP but I'm not sure how. This is what I have... but it don't update both records.
set rsTblEmpl=Server.CreateObject("ADODB.Recordset")
if 1=1 then
sql = "select * from tblEmpl where ssn=" & strSsn & ";"
else
sql = "select * from tblEmpl where eight_id=" & intEight_id & ";"
end if
rsTblEmpl.open sql, conn, 2, 3
if not rsTblEmpl.eof then
rsTblEmpl("posid") = StrPosId
rsTblEmpl("posname") = StrPosName
rsTblEmpl.update
end if
rsTblEmpl.close
set rsTblEmpl=nothing
The reason why it's only updating 1 value is because you're not looping over all records. You're only going to be updating the first record that it finds. So if there are multiple records with the same SSN you will have to change the if statement to a loop.
do until rsTblEmpl.eof
rsTblEmpl("posid") = StrPosId
rsTblEmpl("posname") = StrPosName
rsTblEmpl.update
rsTblEmpl.movenext
loop
Or you can do them all in one go using a parameterized UPDATE statement:
UPDATE tblEmpl SET posid = ?, posname = ? WHERE ssn = ?
The if 1=1 at the top I'll assume is a placeholder, because now it will always be true, so the else is not needed.

What is incorrect in this sql query?

update v, s
set v.closed = 'Y'
where v.closed <> 'y'
and v.canceldate < '12.01.2017'
and s.salesrep1 = 'bd'
and v.orderno = s.orderno
This is my query and I get this error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
The problem is most likely that you mixed up what rdbms product you use. The error message is from MS SQL Server, while the question is (well, was) tagged as mysql.
The syntax used in the question is allowed in mysql, but is not allowed in MS Sql Server, hence the error message.
In MS Sql Server try the following syntax:
update v set v.closed = 'Y'
from v inner join s
on v.orderno = s.orderno
where v.closed <> 'y'
and v.canceldate < '12.01.2017'
and s.salesrep1 = 'bd'
See ms sql server reference on update statement for details
I would pur it like this (on sql server)
update v
set v.closed = 'Y'
From v inner join s
On v.orderno = s.orderno
Where v.closed <> 'y'
and v.canceldate < '12.01.2017'
and s.salesrep1 = 'bd'

How to correctly set expression Query.CommandText in SSRS?

In my SSRS report builder I have an expression set for one of my dataset (name is dsTagAttributes). The query type is text.
This is the query expression
="select m.TagName, ta.descriptor, t.[State] from MasterTags m inner join TagAttributesClassic ta on m.ID = ta.TagID inner join TagStates t on t.ID = m.StateID where m.PIServerID = #ServerID and t.[State] = #State and m.MetaData1 = #Station " & Parameters!AndOr.Value & "m.MetaData2 = #Device"
The above query expression has a parameter which is concatenated in the select statement.
The parameter #AndOr data type is text, has a default value of "or", and parameter visibility is set to hidden (screen shot below). I have set this parameter because it will be used to dynamically change the dataset result from or to and during the report runtime.
But when I run the report, an exception is thrown saying,
What could go wrong in my query command text?
Any help is greatly appreciated.
You can run Profiler against Data Source to see the query.
I assume the problem is that you miss "'" before and after each varchar value (#State, #Station).
I wouldn't use that way to use OrAnd condition
You can modify TSQL like
...
AND
(m.MetaData2 = #Device AND #OrAnd = 'AND')
)
OR
(m.MetaData2 = #Device AND #OrAnd = 'OR')
Just make sure you place ')' and '(' correctly, depending on business logic.

how to for each loop on data table within another datatable

I have a web poll application that reads and processes orders from sql server back end and inserts into windss(jda).
the flow
1) I read all the orders from the sql server tables that is not processed
2) then I need to divide the orders into two sets
- set 1 orders with bundle kit( 1 order with several suborders)
- set 2 is just orders with no bundle kit
3) then after each order is processed I need to update the isprocessed sql field to 1
1) code for reading all orders
Frmmain.vb
dt = WebDatabase.GetAllOrdersFromDatabase()**'this method is below**
For Each drow In dt.Rows
If CDbl(WinDSSStoreNumber) = 123 Or CDbl(WinDSSStoreNumber) = 124 Then
' Store 123 and 124 = customer service - only orders with qualifying source codes
my question is:
the above for each checks if the store is 123 or 124 but now I want to implement another for each loop that reads all the InvoiceHeader_Id that the second field in the datatable and then check if it has a bundle kit or not if it does then process it and update the isprocessed field second last in the datatable else process the orders without any bundle kit and update isprocessed for those as well. please any help is generously appreciated, please ask me any questions in the below comment before marking my question down.
WebData.vb
Public Function GetAllOrdersFromDatabase() As DataTable
DrivePath = "C:\Users\somjething\Documents\files\somefiles\"
Dim ds As DataTable = Nothing
WinDSS.connectionString = ConfigurationManager.AppSettings("WinDSS_Connection").Replace("%DrivePath%", DrivePath)
ds = WinDSS.GetSysMst()
If ds.Rows.Count > 0 Then
WinDSSStoreNumber = ds.Rows(0)("store_no")
End If
Dim dt As New DataTable()
Dim conn As New SqlConnection(ConfigurationManager.AppSettings("WebData_Connection") & ConfigurationManager.AppSettings("WebDataSource"))
Dim cmd As SqlCommand
Dim da As New SqlDataAdapter
conn.Open()
cmd = conn.CreateCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText= SELECT InvoiceDetail_Id, InvoiceHeader_Id, ActualFreightCharge, AmountCollected,
ChargedActualFreight, CollectedExternally, CollectedThroughAR, DateInvoiced, InvoiceNo,
InvoiceType, LineSubTotal, MasterInvoiceNo, OrderInvoiceKey, Reference1, TotalAmount,
TotalTax, isprocessed, storetoprocess
FROM dbo.InvoiceHeader
WHERE (isprocessed = 0) AND (storetoprocess = N'123')
da.SelectCommand = cmd
da.Fill(dt)
conn.Close()
Return dt
End Function
Additoinal info
This is the information given to me by my manager:
loop throught each below
SELECT InvoiceDetail_Id, InvoiceHeader_Id, ActualFreightCharge, AmountCollected,
ChargedActualFreight, CollectedExternally, CollectedThroughAR, DateInvoiced, InvoiceNo,
InvoiceType, LineSubTotal, MasterInvoiceNo, OrderInvoiceKey, Reference1, TotalAmount,
TotalTax, isprocessed, storetoprocess
FROM dbo.InvoiceHeader WHERE (isprocessed = 0) AND (storetoprocess = N'195')
Use the above InvoiceHeader_Id to loop throught all of the order information. Process each Bundle (Kit/)
SELECT InvoiceHeader_Id, LineDetails_Id, LineDetail_Id, OrderLine_Id, GiftFlag, [References],
GiftWrap, IsBundleParent, KitCode, KitQty, LevelOfService, LineSeqNo, LineType,
MaxLineStatus, MaxLineStatusDesc, MinLineStatus, MinLineStatusDesc, MinShipByDate,
OpenQty, OrderHeaderKey, OrderLineKey, OrderedQty, OriginalOrderedQty, OtherCharges,
OverallStatus, PipelineKey, PrimeLineNo, ReceivingNode, RemainingQty, ReqCancelDate,
ReqDeliveryDate,
ReqShipDate, SCAC, ScacAndService, ScacAndServiceKey, ShipNode, ShipToID, ShipToKey,
StatusQuantity, SubLineNo, SubstituteItemID, isprocessed
FROM dbo.OrderLine
WHERE (isprocessed = 0) AND (InvoiceHeader_Id = 13) AND (IsBundleParent = 'Y')
ORDER BY PrimeLineNo, SubLineNo
For each record in the above list query below and add as Zero (0) dollar amount. The cost is associated with the information above record (you may have to query tables to ge the values)
Get the OrderLineKey from the above record and query this below to get the associated sub items.
SELECT TOP (100) PERCENT dbo.BundleParentLine.InvoiceHeader_Id AS BPL_InvoiceHeader_Id,
dbo.BundleParentLine.LineDetails_Id AS BPL_LineDetails_Id, dbo.BundleParentLine.LineDetail_Id AS BPL_LineDetail_Id, dbo.BundleParentLine.OrderLine_Id AS BPL_OrderLine_Id, dbo.BundleParentLine.BundleParentLine_id, dbo.BundleParentLine.SubLineNo AS BPL_SubLineNo, dbo.BundleParentLine.PrimeLineNo AS BPL_PrimeLineNo, dbo.BundleParentLine.OrderLineKey AS BPL_OrderLineKey, dbo.OrderLine.*
FROM dbo.BundleParentLine INNER JOIN dbo.OrderLine ON dbo.BundleParentLine.OrderLine_Id = dbo.OrderLine.OrderLine_Id AND dbo.BundleParentLine.LineDetail_Id = dbo.OrderLine.LineDetail_Id AND dbo.BundleParentLine.LineDetails_Id = dbo.OrderLine.LineDetails_Id AND dbo.BundleParentLine.InvoiceHeader_Id = dbo.OrderLine.InvoiceHeader_Id
WHERE (dbo.BundleParentLine.OrderLineKey = N'76873264832') AND (dbo.BundleParentLine.InvoiceHeader_Id = 13) AND (dbo.BundleParentLine.LineDetails_Id = 6) AND (dbo.OrderLine.isprocessed = 0)
ORDER BY BPL_PrimeLineNo, BPL_SubLineNo
After Processed, set isprocessed = 1
Use the above InvoiceHeader_Id to loop throught all of the order information
SELECT InvoiceHeader_Id, LineDetails_Id, LineDetail_Id, OrderLine_Id, GiftFlag, [References],
GiftWrap, IsBundleParent, KitCode, KitQty, LevelOfService, LineSeqNo,
LineType,
MaxLineStatus, MaxLineStatusDesc, MinLineStatus, MinLineStatusDesc, MinShipByDate,
OpenQty, OrderHeaderKey, OrderLineKey, OrderedQty,
OriginalOrderedQty,
OtherCharges, OverallStatus, PipelineKey, PrimeLineNo, ReceivingNode, RemainingQty,
ReqCancelDate, ReqDeliveryDate, ReqShipDate, SCAC,
ScacAndService,
ScacAndServiceKey, ShipNode, ShipToID, ShipToKey, StatusQuantity,
SubLineNo, SubstituteItemID, isprocessed
FROM dbo.OrderLine
WHERE (isprocessed = 0) AND (InvoiceHeader_Id = 13) AND (IsBundleParent <> 'Y')
ORDER BY PrimeLineNo, SubLineNo
After Processed, set isprocessed = 1
This is not an answer but I want to pose this to prompt you for some clarification and it's not practical in the comments.
First, close visual studio and open SQL Server Management Studio and connect to the database
We'll build some select statements to get the data back and then we can think about an UPDATE statement
This first query you posted gives you the unprocessed headers for store 195 (straight from your post). I don't know if you want to do this for all stores or not. Paste it into SSMS and run it
SELECT H.*
FROM dbo.InvoiceHeader H
WHERE H.isprocessed = 0 AND H.storetoprocess = N'195'
Now, this second query gives you the unprocessed lines against that header that have IsBundleParent='Y'.
Look! No loops!
SELECT H.*, L.*
FROM dbo.InvoiceHeader H
INNER JOIN
dbo.OrderLine L
ON L.InvoiceHeader_Id = H.InvoiceHeader_Id
WHERE H.isprocessed = 0 AND H.storetoprocess = N'195'
AND L.isprocessed = 0 AND L.IsBundleParent = 'Y'
Then in your explanation you have For each record in the above list query below and add as Zero (0) dollar amount which makes no sense to me so perhaps you could clarify
Now we'll add the third query in which adds BundleParentLine (which does have inner joins in it already, but it's a pretty strange list of join fields)
SELECT H.*, L.*, BPL.*
FROM dbo.InvoiceHeader H
INNER JOIN
dbo.OrderLine L
ON L.InvoiceHeader_Id = H.InvoiceHeader_Id
INNER JOIN
dbo.BundleParentLine BPL
ON BPL.OrderLine_Id = L.OrderLine_Id
AND BPL.LineDetail_Id = L.LineDetail_Id
AND BPL.LineDetails_Id = L.LineDetails_Id
AND BPL.InvoiceHeader_Id = L.InvoiceHeader_Id
WHERE H.isprocessed = 0 AND H.storetoprocess = N'195'
AND L.isprocessed = 0 AND L.IsBundleParent = 'Y'
Then in your explanation you have After Processed, set isprocessed = 1, but what is 'processed' here? Is it just setting it to processed in the table?
To do that you just write an UPDATE statement. Here's one way to do that to update just the header (don't run it though until we understand what you're trying to do). Is the problem here that you want to set header and line to processed at the same time?
UPDATE InvoiceHeader
SET Processed = 1
WHERE isprocessed = 0 AND storetoprocess = N'195'
The fourth query you have seems to account for the IsBundleParent <> 'Y' case
Again, we need to know what add as Zero (0) dollar amount and processed means to go any further.

LIKE statement using parametrized SQL in vbscript

I'm trying to create a LIKE SQL statement in vbscript using parametrized SQL.
The command text passed through is along the lines of
SET NOCOUNT ON;
DECLARE #pname as nvarchar(50);
SELECT #pname = ?;
SELECT *
FROM tblProject
WHERE projName LIKE #pname
And I'm concatenating % onto either side of the string that I'm passing through as pname. If I change the LIKE to an = and remove the %s then it works fine. And if I run the statement directly and replace #pname with '%searchterm%' it works fine. Any help would be much appreciated.
Edit: vbscript:
Set rsAnswers = Server.CreateObject("ADODB.Recordset")
Set cmd = server.createobject("ADODB.Command")
cmd.CommandText = mySQL
cmd.CommandType = 1
cmd.CommandTimeout = 900
cmd.ActiveConnection = svrPerformanceConnectionset prm = cmd.CreateParameter("#pname", 129, 1, 50, thisProjName)
cmd.Parameters.Append prm
rsAnswers.CursorLocation = 3
rsAnswers.Open cmd, , 0, 1
(thisProjName is getting a string from a form)
Thanks,
Tim
it is the expected behaviour because the parameter value is escaped to handle it as a value.
the solution is to move the concatenation of the % out of the parameter:
SET NOCOUNT ON;
SELECT *
FROM tblProject
WHERE projName LIKE '%' + #pname + '%';
notice the missing declaration & initialization of the parameter: inizialization and value assignement are performed by the VBScript engine and data access layer when creating the related objects in the script.
After trying many things, I found that the problem was the data type I was declaring my parameter as. I'm not sure why exactly but using adVarChar (200) rather than adChar (129) made the LIKE statement work.
Thanks to everybody who answered and commented!

Resources