Execute saved query - ms-access

My query is below to update SQL server data which is saved on pass through query - "qryPassThroughUpdate".
UPDATE Invoice SET PaidAmount=0, Unpaid=9954.88, Instance = Instance + 1, UpdateByID=10,
UpdateTime='9/24/2020 2:31:10 PM', [_FID_PaymentStatus]=50 WHERE [_ID] = 24123
I got warning message, if I double click query directly and it does update on SQL server.
I want to use VBA code to run query and update SQL server. My code is below.
CurrentDb.Execute "qryPassThroughUpdate"
On the query SQL, it is not SELECT query. Not sure how to run saved query through VBA code.

Related

Use function call in passthrough query?

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.

Sql Server sp_send_dbmail linked mysql server error

execute msdb.dbo.sp_send_dbmail
etc etc
#query = 'select * from Mysql...MemberRef4 as oi where oi.MemberId = 133363 '
The above is a query that I plugged into an existing working send mail method. MySql is a linked MySql database. I CAN run this query without any errors, correctly, by selecting the query code and F5. I am running this code and other MySql code joins with SqlServer and other linked servers - without any errors, including views/procs/etc.
However, the sp_send_dbmail method fails with :
Msg 22050, Level 16, State 1, Line 0
Failed to initialize sqlcmd library with error number -2147467259.
Note: this is the first time I'm trying to put MySql queries in sp_send_dbmail
Any help please. I really don't want stored procs that create temp tables/etc :(

SSIS 2012 - Assigning variables to SQL Statement of an SQL Task

I'm trying to map a parameter and variables to a SQL statement within a SQL Task
I'm connecting to an oracle DB - the connection is working fine.
My SQL Statement is:
insert into ? values(?,?)
I map package parameter: Param1 to (parametername) 0 in the mapping screen
variable1 to (parametername) 1 in the mapping screen and variable2 to (parametername) 3
Both the parameter and variables are datatype VARCHAR, and values are:
p1 = 'TEST_TABLE
v1 = 'TEST'
v2 = 'TEST'
However, I get an error "parameters no mapped correctly"
If I enter a sql statement like: insert into TEST_TABLE values('TEST','TEST') the record is inserted into the oracle database successfully.
Any ideas?
None of the connection managers available to the Execute SQL Task support parameterizing the table name, which is what you're trying to do with the first "parameter" in your query insert into ? values(?,?) (For further details, see "Using Parameter Names and Markers" in the MSDN article Parameters and Return Codes in the Execute SQL Task.)
The generally acceptend solution is to build the insert string on the fly and execute it. Here's an example: Using Tablename as variable in ssis

SQL Task - Set Package variable

I have declared a variable at the package level compdate and am testing data flow to the variable by droping an Execute SQL Task in the Control Flow of the package.
In the task,
SQL Statement:
select ? = (getdate() - 1)
Parameter Mappings:
Variable Name: User::compdate
Direction: Output
Data Type: DATE
Parameter Name: 0
Parameter Size: -1.
Why am i getting error:
[Execute SQL Task] Error: Executing the query "declare #compdate date
set #compdate = (getdate() ..." failed with the following error: "Syntax error or access violation". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
I do not see why you need to execute an SQL statement to get the previous day as this can be done in various other ways.
To answer your question though, since you are trying to store the result of the SQL query from your Execute SQL Task you have to change the SQL statement that you have provided.
Your new query:
SELECT (GETDATE() - 1) AS DateVar
Where DateVar will be the single parameter that is returned which you need to map to your variable.
You need to delete your Parameter Mappings as they are not needed. Open up the Result Set tab and Add a new result. Set the Result Name to be DateVar and set the Variable Name to be your variable User::compdate
You then need to set up your Execute SQL Task to return a Single Row result set in the General tab, mapped to your variable. Select Single row for the ResultSet option.
Working with result sets is explained in great details here. Scroll down to the 'Working with a Single-Row Result Set' section, it has a great example which you can follow.
If you want to use without using the result set. try with following steps.
Create the stored procedure in your respective database. Following
code is an example.
CREATE proc GetYesterDay(#yesterday datetime output)
as
Select #yesterday=getdate()-1
Create the ADO.NET connection to run the stored procedure. In which, you can mention the direction of the input and output of the parameters.
Create the execute task and configure it as following screenshot.
Click on Parameter Mapping and configure as following screenshot.
Now SSISCompletedDate variable will be filled with respective data.
Hope this helps!

SSIS Execute SQL Task SQL Command Issue

I have the following insert statement in my execute sql task in SSIS:
INSERT INTO dbo.SSISLogTest
(NodeID, BusinessDate, StartDate, StopDate, StepName, RecordCount, Message, Status, UserID)
VALUES (?,?,?,?,?,?,?,?,?)
When I run it within the task providing parameters it executes fine and inserts a record in the table.
When I run the package, this step fails with the following error:
"[Execute SQL Task] Error: Executing the query "INSERT INTO
dbo.SSISLogTest
..." failed with the following error: "Parameter name is unrecognized.". Possible failure reasons: Problems with the
query, "ResultSet" property not set correctly, parameters not set
correctly, or connection not established correctly. "
Different connection providers require different syntax - all of the following must be set correctly:
The connection type (i.e. OLE DB, ADO...) Your choice, but aim to use the same throughout your application.
The number and specifics (Variable Name, Direction, Data Type, Parameter Name, Parameter Size) of parameters on the "Parameter Mapping" dialog.
The parameter syntax in the SQL query (i.e. your question marks.)
Please see an OLEDB example in the screenshots below and refer to Working with Parameters and Return Codes in the Execute SQL Task for details.