Create Stored Procedure in Access 2007 - sql

Is it possible to create an Stored Procedure in Access 2007? If it is, how can i do it?
Thanks

You could save queries with parameters that works much like a SP.

You can created stored procedures in Access. I use Access 2007. I'm not sure of the capability in Access 2003. I've created stored procedures using the following process:
Create a query that performs the action you wish to take (Create(MakeTable), Read(Select), Update, Delete). Setup parameters if used. This query is for use as a template. You don't need to keep.
Switch query to SQL View and copy the query.
Use Cntl-G to switch over to Visual Basic. Use either the immediate window or create a subroutine using Insert | Procedure.
Paste your query and setup the following syntax to create the stored procedure.
CurrentProject.Connection.Execute "CREATE PROC sp_storedProcedureName (parm1 Text, parm2 Integer, whatever other parms & datatypes) AS sqlStatementFromQueryGoesHere;"
Run from immediate window or procedure. It should run clean if no errors.
Then go back to the Navagation pane and refresh (the stored proc won't show originally without refresh). Once refreshed, you have your stored proc showing in the queries category

Related

How do I pass parameters into a stored procedure run in a pass through query in access

I have a few linked tables between Microsoft Access and SQL Server. I implemented Access to do all the work in the queries for the database but after further review it was just to slow so I decided to run all the queries in SQL Server by creating stored procedures and have Access just executing a stored procedure as a query. The problem is that some of these stored procedures require parameters. Therefore the code in Access looks like this:
Exec getConsumption #Value1 = Product1
#Value 1 is the parameter needed and Product1 is what I want to pass into the parameter
This all seems to work but is there a way to prompt a user for a text box so that whatever they enter will be passed into the parameter rather than having to go hard code it in every time it needs to be changed ?

Using MSDB stored procedures in application's database stored procedure

This seems like it would be trivial, but I have not been able to come up with a solution to this small problem.
I am attempting to create a stored procedure in my application's database. This stored procedure just executes a job that has been set up in the SSMS on the same server (seemed to be the only way to programmatically execute these jobs).
The simple code is shown below:
USE ApplicationsDatabase
GO
CREATE PROCEDURE [dbo].[procedure]
AS
BEGIN
EXEC dbo.sp_start_job N'Nightly Download'
END
When ran as is, the procedure technically gets created but cannot be executed due to it not being able to find the 'sp_start_job' since it is using the ApplicationsDatabase. If I try to create the procedure again (after deleting previously created) but updating the USE to MSDB, it tries to add it to that system database for which I do not have permissions to do. Finally, I attempted to keep the original create statement but added the USE MSDB within the procedure (just to use the 'sp_start_job' procedure), but it would error saying USE statements cannot be placed within procedures.
After pondering on the issue for a little (I'm obviously no SQL database expert), I could not come up with a solution and decided to solicit the advice of my peers. Any help would be greatly appreciated, thanks!
You will have to fully qualify the path to the procedure. Of course, you can only execute this is the application has permissions.
Try this:
USE ApplicationsDatabase
GO
CREATE PROCEDURE [dbo].[procedure]
AS
BEGIN
EXEC msdb.dbo.sp_start_job N'Nightly Download'
END

New SQL Stored Procedure not showing in Object Explorer

I am using SQL Server 2005 on a client site. In the past I have created stored procedures in it with no problem. The last stored procedure I created does not show up in Object Explorer under "Stored Procedures". I tried saving it with and w/o dbo.
If I try to save another new stored procedure, the name of the one that is not displayed in Object Explorer appears as an option in the "Save File As" picklist.
It will be added to the bottom of the tree when you execute/add the stored procedure. Refresh the Stored Procedure folder in the tree for it to sort it into alpha order.
Also ensure you are creating the Stored Procedure on the database by executing the code with Create Procedure before the procedure name. If you then need to edit the procedure simple modify (right click the procedure in the explorer tree) and change Create Procedure to Alter Procedure.

How to use synonym stored procedures in ssrs?

I want to make a SSRS report using a stored procedure from another sql database.
I made a synonym via SQL Server:
Use DataBase1;
CREATE SYNONYM StoredProcedure1 FOR DataBase2.dbo.StoredProcedure1;
go
In SSRS Design I could not find "StoredProcedure1" in DataBase1. How I can access it?
My fellow colleague in the same team got stuck at the same situation and no solution seems working. So my solution was the, create sp in the database which directly used by SSRS and call the synonym inside that sp. Worked there correctly. But need to know proper solution.
The given case create new SP like here.
USE [DataBase1]
CREATE PROCEDURE New_SP
AS
BEGIN
EXEC StoredProcedure1
END
GO
Then use the newly created sp in SSRS.

sql server trigger to update another database whenever a sproc is created

Is there a way to update another database with the newly created stored procedure whenever a stored procedure is created in the main database?
For example, i have two databases, DB1 and DB2.
When I create a stored procedure in DB1, i want that same procedure to created in DB2 automatically? Is there a trigger that can do this?
USE [DB1]
CREATE PROCEDURE [dbo].[TestSproc]
..something
AS
BEGIN
...something
END
I know a use [DB2] statement will do the job, but i want this to be done automatically. Any thought?
Thanks for the help!
This might be a bit evil, but in SQL Server you are able to create DDL triggers which fire when you create/alter/drop tables/procedures etc. The syntax to fire when a procedure is created is:
CREATE TRIGGER <proc-name>
ON DATABASE
FOR CREATE_PROCEDURE
AS
--Your code goes here
In a DDL trigger you get access to an object called EVENTDATA. So to get the text of the procedure you created:
SELECT EVENTDATA().value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','nvarchar(max)')
Now all you need to do is keep that value and execute it in your secondary database. So your query becomes something like this, though I leave the code to update the secondary database down to you as I don't know if it's on the same server, linked server etc.:
CREATE TRIGGER sproc_copy
ON DATABASE
FOR CREATE_PROCEDURE
AS
DECLARE #procedureDDL AS NVARCHAR(MAX) = EVENTDATA().value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','nvarchar(max)')
--Do something with #procedureDDL
As this is not tested, there's probably a few gotchas. For example, what happens if you create procedure with full name (CREATE PROC server.database.schema.proc)
No simple solution for this unless you want to execute the same statement twice, once on each target database,
One thing that comes to my mind is that you can Set up Replication and only publish Stored Procedures to your second database which will be the subscriber in this case.
Following is the window where you select which Objects you want to send over to your secondary databases.