I have a lot of such assignment statements let $tmp :=, because otherwise I get the error message "Incomplete FLOWR Expression, expecting return":
declare function module:import($xmlsrc as xs:string, $conn as xs:anyURI) {
let $root := module:getXml($xmlsrc)
(: Table01 :)
let $id := 2
let $tmp := trace("Table01")
let $sqlstmt := "insert into Table01 (rel1,rel2,rel3,rel4,rel5) values("
|| $id || ",
'" || $root//myns:ftr[#n='abc']/myns:ftr[#type='p1'] || "',
'" || $root//myns:ftr[#n='abc']/myns:ftr[#type='p2'] || "',
'" || $root//myns:ftr[#n='abc']/myns:ftr[#type='p3'] || "',
'" || $root//myns:ftr[#n='abc']/myns:ftr[#type='p4'] || "')"
let $tmp := sql:execute($conn, $sqlstmt)
(: Table02 :)
let $id := 3
let $sqlstmt := "insert into Table02 (rel1,rel2,rel3,rel4,rel5) values("
|| $id || ",
'" || $root//myns:div[#n='abc']/myns:xyz[#type='p5'] || "',
'" || $root//myns:div[#n='abc']/myns:xyz[#type='p6'] || "',
'" || $root//myns:div[#n='abc']/myns:xyz[#type='p7'] || "',
'" || $root//myns:div[#n='abc']/myns:xyz[#type='p8'] || "')"
let $tmp := sql:execute($conn, $sqlstmt)
(: more tables to import to :)
return()
};
Is there another way, i.e. without those assignment statements, to meet the XQuery requirements?
It looks as if you want a function that is called only for its side effects, and you don't actually want to return anything from the function.
Functions with side-effects aren't well supported by the XQuery language specification, and you're very much in "vendor extensions" territory. So the answer is going to depend on which XQuery implementation you are using.
You can call the functions without binding the results to variables, for example
declare function (...) {
module:getXml($xmlsrc),
trace("Table01"),
sql:execute($conn, "insert into Table02 (rel1,rel2,rel3,rel4,rel5) values("
|| $id || ",
'" || $root//myns:div[#n='abc']/myns:xyz[#type='p5'] || "',
'" || $root//myns:div[#n='abc']/myns:xyz[#type='p6'] || "',
'" || $root//myns:div[#n='abc']/myns:xyz[#type='p7'] || "',
'" || $root//myns:div[#n='abc']/myns:xyz[#type='p8'] || "')"),
sql:execute($conn, "insert into Table02 (rel1,rel2,rel3,rel4,rel5) values("
|| $id || ",
'" || $root//myns:div[#n='abc']/myns:xyz[#type='p5'] || "',
'" || $root//myns:div[#n='abc']/myns:xyz[#type='p6'] || "',
'" || $root//myns:div[#n='abc']/myns:xyz[#type='p7'] || "',
'" || $root//myns:div[#n='abc']/myns:xyz[#type='p8'] || "')")
};
With both formulations the language spec offers no guarantee about order of execution, which is rather critical in a case like this; for such guarantees you will need to turn to vendor documentation.
Related
Private Sub cmdAdd_Click()
'add data to table
CurrentDb.Execute "INSERT INTO Products(ProductCode, ProductName, Category, UnitPrice, UnitCost, QuantityInStock, SupplierCode) " & _
" VALUES (" & Me.txtProductCode & " , '" & Me.txtProductName & "' , '" & Me.cboCategory & "' , '" & Me.txtUnitPrice & "' , '" & Me.txtUnitCost & "' , '" & Me.txtQuantityInStock & "' , '" & Me.cboSupplierCode & "')"
'refresh data on list of form
ProductsSub.Form.Requery
End Sub
the code above is suppose to record the data that has been input from the interface, but it does not do anything. The same format of code was used in a different form and it worked. Really weird, but i don't how it was possible.
You'll need to debug this. One of the values you are referencing visa the Me object is not resolving. Place a breakpoint on the CurrentDB.Execute command, and hover over each Me reference (i.e. Me.txtProductCode) to make sure it is obtaining the correct value for the insert. If you have moved this to a library, you may want to use CodeDB.Execute instead of CurrentDB.Execute. I would also check the table to see if the actual INSERT is working as there may be an issue with the Requery event.
Hi I would like to know how I can fetch the records when I'm using parameter file.
My scripts is like that. when I execute it, I got error message. Please help me to fix this script.
Thank you in advance
CREATE OR REPLACE PROCEDURE testappt
(
XFILE IN VARCHAR2
) is
xfpt varchar2(1):='F';
TYPE curtype IS REF CURSOR;
appt_cur curtype;
appt_rec appt_cur%ROWTYPE; -- error
BEGIN
open appt_cur for 'SELECT * FROM ' || xfile || ' where fpt!= :xfpt ' using xfpt;
loop
fetch appt_cur into appt_rec -- error
exit when appt_cur%not found; -- error
execute immediate 'update ' || xfile || ' set apptgrp=46' || 'where reptrc=6269' ||
'and og=trim(0||6)' || 'and trim(a_jc)=2876';
commit;
end loop;
end testappt;
/
It looks like you need some spaces in your literals. Try:
CREATE OR REPLACE PROCEDURE testappt
(
XFILE IN VARCHAR2
) is
xfpt varchar2(1):='F';
TYPE curtype IS REF CURSOR;
appt_cur curtype;
appt_rec appt_cur%ROWTYPE; -- error
BEGIN
open appt_cur for 'SELECT * FROM ' || xfile || ' where fpt!= :xfpt ' using xfpt;
loop
fetch appt_cur into appt_rec -- error
exit when appt_cur%not found; -- error
execute immediate 'update ' || xfile ||
' set apptgrp=46' ||
' where reptrc=6269' ||
' and og=trim(0||6)' ||
' and trim(a_jc)=2876';
commit;
end loop;
end testappt;
/
Share and enjoy.
ACCEPT p_username PROMPT 'Enter Username : '
ACCEPT p_password PROMPT 'Enter New Password for Username : '
VARIABLE g_output VARCHAR2(4000)
DECLARE
CURSOR NAME IS SELECT TABLE_NAME FROM DBA_TABLES
WHERE OWNER LIKE '%&p_username%';
DDL_DROP VARCHAR2(200);
BEGIN
FOR TNAME IN NAME
LOOP
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE' || ' ' || TNAME.TABLE_NAME;
:g_output := :g_output || ' ' || TNAME.TABLE_NAME;
END;
END LOOP;
END;
/
PRINT g_output
Hello, I'm new to PL/SQL and trying to make a script to drop the user's table and ultimately change their password later after dropping their tables. I am having difficulty with the EXECUTE IMMEDIATE command. The script works if I remove the EXECUTE IMMEDIATE line. I tested it by printing the table names inside the loop and I get the right # of tables and their corresponding names.
Any help is appreciated, thanks.
Edited the code to reflect the suggestion but still didn't work. Getting the same error.
ACCEPT p_username PROMPT 'Enter Username : '
ACCEPT p_password PROMPT 'Enter New Password for Username : '
VARIABLE g_output VARCHAR2(4000)
DECLARE
NAME SYS_REFCURSOR;
DDL_WORD VARCHAR2(200);
BEGIN
OPEN NAME FOR SELECT TABLE_NAME FROM DBA_TABLES
WHERE OWNER LIKE '%&p_username%';
LOOP
FETCH NAME INTO DDL_WORD;
EXIT WHEN NAME%NOTFOUND;
EXECUTE IMMEDIATE 'DROP TABLE "' || DDL_WORD || '" CASCADE CONSTRAINTS';
:g_output := :g_output || ' ' || DDL_WORD;
END LOOP;
CLOSE NAME;
END;
/
PRINT g_output
You probably need to specify the owner for the table in the DROP statement:
ACCEPT p_username PROMPT 'Enter Username : '
ACCEPT p_password PROMPT 'Enter New Password for Username : '
VARIABLE g_output VARCHAR2(4000)
DECLARE
CURSOR NAME IS SELECT OWNER, TABLE_NAME FROM DBA_TABLES
WHERE OWNER LIKE '%&p_username%';
DDL_DROP VARCHAR2(200);
BEGIN
FOR TNAME IN NAME
LOOP
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE' || ' ' || TNAME.OWNER || '.' || TNAME.TABLE_NAME;
:g_output := :g_output || ' ' || TNAME.OWNER || '.' || TNAME.TABLE_NAME;
END;
END LOOP;
END;
/
PRINT g_output
The code looks fine.
You could try with () like this
BEGIN
EXECUTE IMMEDIATE (code_text);
END;
You could try
c SYS_REFCURSOR;
BEGIN
OPEN c FOR 'SELECT * FROM table';
CLOSE c;
END;
I have a web app with a form that I am trying to pass to an ASP.NET server (using VB.NET) and then on to a MS SQL Server table. The form uses a jQuery datepicker in several textboxes and formats them as MM/dd/yyyy. The form fields are then passed through a PageMethod to the web server which takes the various field values and combines them into a SQL UPDATE command.
I am constantly getting the following error whenever I try to execute the SQL command:
Conversion failed when converting date and/or time from character string.
Here is the code on the server:
Using myConn As New System.Data.SqlClient.SqlConnection(CString.ConnectionString)
myConn.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand("UPDATE table " & _
"SET type = '" & type & "', " & _
"target = '" & "#target" & "', " & _
"patient = '" & patient & "', " & _
"dob = '" & "#dob" & "' " & _
"WHERE serial = '" & serial & "'", myConn)
cmd.Parameters.Add(SqlParameter("#target", Data.SqlDbType.Date))
cmd.Parameters.Add(SqlParameter("#dob", Data.SqlDbType.Date))
If target = "" Then
cmd.Parameters("#target").Value = Data.SqlTypes.SqlDateTime.Null
Else
cmd.Parameters("#target").Value = target
End If
If dob = "" Then
cmd.Parameters("#dob").Value = Data.SqlTypes.SqlDateTime.Null
Else
cmd.Parameters("#dob").Value = dob
End If
cmd.ExecuteNonQuery()
End Using
Note: I've tried about twenty different ways of parsing the dates, converting them to dates, changing around the formats and none of it has worked.
Note 2: The conditional statements at the end are simply to prevent empty date fields from being stored in the SQL DB as "1/1/1900", but rather as an actual SQL NULL value. From debugging though, it seems that this is not the issue - it is when there is an actual value that the error is fired.
If anyone can see what I'm doing wrong and how I might fix it, it would be greatly appreciated. Thanks in advance for your help!
You are mixing up your parameterized and non-parameterized parts (why aren't you parameterizing everything?)
Dim cmd As New System.Data.SqlClient.SqlCommand("UPDATE table " & _
"SET type = '" & type & "', " & _
"target = #target, " & _
"patient = '" & patient & "', " & _
"dob = #dob " & _
"WHERE serial = '" & serial & "'", myConn)
Are you including time? DateTime fields require date and time.
I am trying to excute this sql query
Dim str As String = "UPDATE table1 SET " & _
"number = '" & strc & "'," & _
"code = '" & "123" & "'," & _
"line= '" & dd1.text & "'," & _
"sellr = '" & txtrun.text & "'," & _
"endu= '" & txtex1.value+txtex2.value & "'" & _
"WHERE number IN (select table1.number" & _
"FROM table1 INNER JOIN table2 ON table1.number = table2.number" & _
"WHERE ((table1.username)='" & session("username") & "' AND (table1.pass)='" & session("pass") & "' AND (table2.sellnum)='" & session("sellnum") & "'));"
there is a Syntax error in query expression and this is te first time I am using nested subquery
all the field are getting String values
So if someone can tell me what is the right approach to write this query I will be very grateful
You're missing spaces after table1.number and table2.number fields in the subquery.
I don't know where you're using this query, but you might want to read about SQL injection. When you stick strings together to build SQL, your code may be vulnerable to malicious users who put SQL code into the fields of your application.