[SOLVED] Passing 2 variables to a SQL Azure Stored procedure |
11/4/2021 3:33:28 PM |
PHPRunner General questions | |
![]() Dear All I have a stored procedure in SQL Azure that accepts two arguments I would normally execute it by writing
from the how to documents I cam across the following Executing an SQL Server stored procedure from events which states that the way to run code with one parameter would be as follows $sql = DB::PrepareSQL( "EXEC StoredProcNameHere ':1'", $values["FieldName"] ); Question is this $sql = DB::PrepareSQL( "EXEC auditselection ':2'", $values["FieldName1"], $values["FieldName2"]); |
|
![]() |
Dalkeith author 11/4/2021 |
As ever writing down my thoughts helps me understand them... I think SERVER code will be as follows $sql = DB::PrepareSQL( "EXEC auditselection ':1',':2'", $values["FieldName1"], $values["FieldName2"]); I deduced from below that :1 is actually a place holder for the following value so I am planning on inputting two variables so I will need two place holders. Somewhat confusingly the way you escape a string or escape code in SQL can be with a single quote... |
![]() |
Admin 11/4/2021 |
Yes, the following is a correct syntax, assuming that both first and second parameters are text values: $sql = DB::PrepareSQL( "EXEC auditselection ':1',':2'", $values["FieldName1"], $values["FieldName2"]); |
![]() |
Dalkeith author 11/5/2021 |
Thanks Xline ....and can I just check? Would the CLIENT BEFORE be something like values["FieldName1"] = row.getFieldValue("FieldName1") |
![]() |
Admin 11/5/2021 |
You need to explain your question fully as the latest code didn't make much sense to me. You are not even saying what page is this, is this ClientBefore event of a button or of a field event etc. |
![]() |
Dalkeith author 11/5/2021 |
Apologies - I was thinking about having a button in each line of the list view that will take the values of two fields in the row and be pass those to the stored procedure and the above code is aimed at being placed in the CLIENT BEFORE event. |
![]() |
Dalkeith author 11/6/2021 |
I nearly had it... On the CLIENT BEFORE I had to alter the word values to params like so params["Firstname"] = row.getFieldValue("Firstname"); Where Firstname and DateCreated are field values in my table T0001Persons So in Javascript I am passing the field values to parameters called Firstname and Dcreate and then in PHP I pass those variables to the stored procedure and the database procedure does the rest. and so I had to alter the syntax of the DB::PrepareSQL to... $sql = DB::PrepareSQL( "EXEC auditselection ':1',':2'", $params["Firstname"], $params["Dcreate"]); The above works but I am told from reading that it is better to indicate the schema and there should be a performance boost to that. $sql = DB::PrepareSQL( "EXEC dbo.auditselection ':1', ':2'", $params["Firstname"], $params["Dcreate"]); You can also reference the variables - I don't know if there is an advantage to this one but I include for completeness $sql = DB::PrepareSQL( "EXEC dbo.auditselection @firstname=':1', @Dcreate=':2'", $params["Firstname"], $params["Dcreate"]); And this is the syntax of the actual storedprocedure in SQL Azure.. where you can see the variables that I happen to have called @firstname and @Dcreate although they could have been named anything because PHP is passing the values and not the memory location(variable name) which makes sense because you are passing from the web server to the database server and the same variables don't exist in both. USE yourdatabasename |