I have a passthrough query in an Access 2010 application, which I'm using to call a stored procedure on a SQL Server backend. The stored procedure takes a parameter that I need to make dynamic. The problem is this:
Execute spMyProc 'userName' works as expected.
Execute spMyProc getUserName() generates a "syntax error at ')'" message.
Is it possible to use a function as a parameter in a pass-through query?
Also, I should note that I'm migrating a complex Access application to SQL server, and I'm really not well-versed in what I'm doing. Any suggestions on things I'm doing incorrectly will be gratefully received. This particular question is rising from an attempt to change the Record Source of a Form from a simple select statement in the Record Source property to something that can be run on the server.
You can use this code:
With CurrentDb.QueryDefs("MyPass")
.SQL = "exec spMyProc '" & getUserName() & "'"
.Execute
End With
Because getUserName() is a local VBA function, then you need to pre-evaluate the actual string sent to SQL server. As above shows using a saved pass-though is "handy" since you don't have to deal with connection strings etc.
Related
I wrote a little SQL command to correct a field in a table. Since it was so small (and maybe I got a little arrogant) I didn't run it even once, and just put it into an update package for a different user.
Dim SQL As String
Dim rs As DAO.Recordset
On Error GoTo errhandler
SQL = "UPDATE Table1 SET Name = 'Calender' WHERE Name = 'Clalender'"
CurrentDb.Execute
errhandler:
Exit Sub
That's why I didn't notice it should have been
CurrentDb.Execute (SQL)
When the user started this command, Access said something like "Critical Error" and closed.
How come the error handling didn't catch that error? And why didn't Access tell me there was something missing when I wrote it? Usually it's quite pedantic about that.
The statement
CurrentDb.Execute
will not compile.
Compile error: Argument not optional.
Your victim ;) must have hit the error when they tried to run the code, which triggered an "on-the-fly" compilation. Error trapping in your code would not handle that because your code never got a chance to run (because it wouldn't compile).
In other words, Access would have gotten all pedantic on you if you had tried to run (or at least compile) your code.... :)
Try to compact and repair the database.
If that does not work, create a new empty database and import all the objects from the problematic database. Perhaps, you can import a few objects each time to determine which is the corrupted object (if any. Sometimes Access have this behavior and there isn't any bad object).
We are using SQL Server 2012 and MySQL 6 and though I can write a SQL statement directly in the Dataset window, I cannot seem to get a MySQL Stored Procedure to work by passing parameters via CALL:
Actuaaly my problem is
either SSRS or the ODBC driver is having a problem sending/receiving the parameter value.
Other attempts at syntax:
call shop.GetRegions() ,
call shop.GetRegions(?) ,
call shop.GetRegions(regid) ,
call shop.GetRegions(#regid)
None of these worked.
If I call the procedure with a hard coded value i.e. call shop.GetRegions(5) - it works.
Again, if the stored procedure has no parameters it works fine.
Can anyone give me any real direction on this issue. A sample of the correct syntax or a step by step example would really be appreciated.
Finally I got solution struggled by couple of days.
We should create linked server in SSMS from MYSQL and then only we need to call the stored procedure.
Syntax to call stored procedure:
EXEC ('CALL GASP_sales_aps(?, ?)', #dt_start, #dt_end)AT MySQL(Linked Server Name)
GASP_sales_aps ---->procedure Name
(#dt_start,#dt_end)------> are parameters .
If you don't want to use a linked server you can
set the dataset type to be Text
set the query to
="exec GASP_sales_aps'," & Parameters!#parameter1.Value & "','" & Parameters!#parameter1.Value &"'"
#RegBes is correct instead of linked server set the dataset type to Text. small workaround in syntax add the parenthesis and instead of exec need to use call for mysql.
="call GASP_sales_aps('," & Parameters!#parameter1.Value & "','" & Parameters!#parameter1.Value &"')"
Good people,
I am trying to execute an SQL query against an MS Access database through my Java program. The program uses the JdbcOdbc driver. The query contains the inbuilt Replace function in it. It looks something like this:
SELECT Replace(first_name, '-', ' ') AS f_name FROM patient WHERE....
The program fails with the following error: "[Microsoft][ODBC Microsoft Access Driver] Undefined function 'Replace' in expression."
How can this be? Other functions such as LCase and Trim work just fine. Also, the Replace function works okay when executed directly on the back end.
Your query can not use the Replace() function unless it is run from within an Access session.
The situation is the same as if you were attempting to use a user-defined function. The db engine can only utilize those with assistance from the Access expression service, which is only available from within an Access session.
I have to connect to many Foxpro databases that have a connection string like:
Data Source=\\All Users\\DB0009\db1.dbc;User ID=xxx;Password=yyy;Provider=VFPOLEDB.1;Persist Security Info=True;
where the folder DB0009 can be any integer from 0000 to 0100 and the db1.dbc can be either db1.dbc or db2.dbc for each folder.
For each connection string, I need to build a simple SQL statement that is identical for every database except for a hardcoded year. So the sql is: select *, '2012' from table
I'd like to be able to store both the connection string AND the year for each connection string in a sql table that can be looked up at run time.
I am using SSIS 2008. I'm guessing based on what i've seen that i can use the foreach loop with the enumerator set as the ADO Enumerator (though i wouldn't mind confirmation there), but how can i pull the year off of that same table and put it into the sql query i have inside a data flow task?
Thanks for any help!
Kary
You could do the following steps:
Create the foreach loop to get the connection string and year values into variables.
create a connection manager and, using an expression, set the connection string property to your connection string variable.
Create another variable with an expression that creates your SQL command with the year variable (dynamic SQL command string).
use the execute SQL task to execute this within your foreach.
Here's a link to a blog on mapping the parameter inside the Execute SQL Task that should be helpful. http://www.rafael-salas.com/2007/11/ssis-mapping-parameter-inside-of.html
I am using the RODBC package to query for results in my SQL server. I have a certain stored procedure written that, when executed in my SQL Server Mgmt. studio (for example), returns a table. However, when I run the query through R, it returns character(0)
# Execute command...
sqlQuery(production,"exec port.tdp_RISK2_ModelRunCompare #ModelRunId1 = 399")
Weird thing is... when I do something like...
sqlQuery(production,"exec sp_who")
I get a table of results...
Help?
I had the same problem.
You can try using:
set nocount on
in the MS SQL Server stored procedure so it will return just a dataset.
Regards,
Try this:
sqlQuery(production,"exec port.tdp_RISK2_ModelRunCompare #ModelRunId1 = 399", errors=FALSE)
Also try writing to a new data frame. I kept getting character(0) when I tried to overwrite an existing data frame with the sqlQuery function.