I've got a stored procedure which takes two parameters. I'm trying to use OpenQuery in SQL Server to join on the result set of my stored procedure dbo.TwoDrugs. The SQL Server is only a local machine with no one else having access. I'm trying to do
select *
from OpenQuery (AHCTW208D02,'exec [i 3 sci study].dbo.TwoDrugs ''X'',''Y''')
and I get the error
Msg 7357, Level 16, State 2, Line 1
Cannot process the object "exec [i 3 sci study].dbo.TwoDrugs 'X','Y'". The OLE DB provider "SQLNCLI10" for linked server "AHCTW208D02" indicates that either the object has no columns or the current user does not have permissions on that object.
I can't think of a reason why I wouldn't have permission (since I created the stored procedure), and I configured the server for data access.
When I run the stored procedure it does in fact return a result set. Any ideas on what might be the problem with this?
OPENQUERY cannot be used to execute extended stored procedures on a linked server. However, an extended stored procedure can be executed on a linked server by using a four-part name. For example:
exec AHCTW208D02.[i 3 sci study].dbo.TwoDrugs 'X','Y';
please read Remaks
Reference: http://msdn.microsoft.com/en-us/library/ms188427.aspx
Related
I have a stored procedure on tempdb database (under System Databases) on server A. The stored procedure has 3 parameters, Param1, Param2, and Param3 which all accept varchars.
I would like to execute this stored procedure on server B for a database called SomeDB.
With the stored procedure, I'd like to pull data from different tables on SomeDB, and then put the results in a new table that will be created called SomeNewTable, which again will be located on SomeDb
Let's assume that the servers are linked.
How should I approach this?
Write the stored procedure on ServerB . Test the stored proc so that it can run completely on ServerB when you are connected to server B. So everything is happening in Server B.
Now just call the Server B stored proc from Server A.
On Server B exec SomeDb.dbo.myStoredProc(p1,p2,p3)
On Server A exec LinkedServerB.SomeDb.dbo.myStoredProc(p1,p2,p3)
Do not use tempDb for anything - you can not reliably put a stored proc in TempDb*
The fact that it will work in temp db for awhile is misleading.
If you are doing a coding exercise they may have you use tempDb but that is a special situation.
You will have to deal with the security jump from ServerA to ServerB depending on how the linked server was setup.
*(yes I know how to do it permanently - but I doubt it works in azure)
I am using R to run a stored SQL procedure:
query.str = "EXEC [StoredProcedure].[Procedure1]"
con <- odbcConnect("my_database")
my_data = sqlQuery(con, query.str)
This code works fine on my laptop. But when I try to run it on the server it gives an error:
42000 2812 [Microsoft][ODBC SQL Server Driver][SQL Server]Could not find stored procedure
I don't think this is a problem with the stored procedure itself, as I have encountered the same situation with multiple stored procedures (they work on my laptop but not the server).
Edit: I am sure the connection string works. When I use the same connection string for a non-stored-procedure, it works and data reads in just fine. The problem only occurs with stored procedures.
Thank you in advance!
Solution found: going through the Window Odbc connector, changing the default database to be the desired database fixed the problem.
This error may raise due to three main issues:
Incorrect reference of object's encapsulation including schema or database.
Every SQL Server object (table, stored procedure, function, etc.) resides in a schema and every schema resides in a database. Also, every object can be referenced by multi-part names. The default schema in SQL Server is dbo. Therefore by not specifying the database and schema in object reference, object is assumed to reside in connecting database and dbo schema. Consequently, below calls are equivalent:
EXEC [myServer].[myConnectedDatabase].[dbo].[myStoredProcedure]
EXEC [myConnectedDatabase].[dbo].[myStoredProcedure]
EXEC [dbo].[myStoredProcedure]
EXEC [myStoredProcedure]
If myStoredProcedure does not reside in either specified database or schema, this error would raise. If you do not know or remember where stored procedure resides, run queries on system sys views, INFORMATION_SCHEMA views, or system stored procedures, sp_*.
Incorrect spelling of stored procedure including not escaping special characters or reserved words.
To escape spaces, special characters (non-alphanumeric and non-underscore), and reserved words, enclose object names in square brackets [...]. Even better avoid such names. Thankfully for you, by default SQL Server is not case sensitive regarding identifiers. In other RDBMS's, like Oracle and Postgres, case sensitivity is retained for mixed cases during CREATE TABLE stage and double quotes would be needed for mixed cases types (i.e., "myStoredProc" <> mystoredproc <> MYSTOREDPROC).
Non-existent object in database or schema either by deletion or transfer to a different database or schema.
I am trying to write an MS SQL statement to fetch a row.
SELECT otherfields, phantom_col FROM mytable WHERE id=5
The above SQL fails with error:
Msg 207 Level 16 Stage 1, Line XX
invalid column name 'phantom_col'.
I loaded SQL Management Studio 2008 R2 and connected to the said database and table and did a "select top 1000 rows" to get the auto generated SQL. It shows:
SELECT TOP 1000 [otherfields], [phantom_col] FROM [mydatabase].[dbo].[mytable]
I then deleted the part that reads [mydatabase] and immediately SQL Management Studio tells me [phantom_col] is invalid.
What special kind of column is that phantom_col? Strictly speaking, if I omit the lengthy [].[] notation, is my SQL syntax still correct?
EDIT: I looked finally looked closely enough and realised there is an error message. edited as above.
Your syntax is correct.
The error, because you haven't connect to the myDatabse
you can use this too
USE mydatabase
GO
SELECT TOP 1000 phantom_col
FROM myTable
When a SQL Server login connects to SQL Server, the login is
automatically connected to its default database and acquires the
security context of a database user. If no database user has been
created for the SQL Server login, the login connects as guest. If the
database user does not have CONNECT permission on the database, the
USE statement will fail. If no default database has been assigned to
the login, its default database will be set to master.
the quote is taken from here
I have SQL server and It has another SQL server Linked server [DEV]. Now I want call a stored procedure in Linked server and get the result value. It is always giving error. I tried below both statements none of the did not worked.
ALTER PROCEDURE [dbo].[UpdateMemebership_Lock_Count]
#v_constit_id_in as varchar(100)
AS
BEGIN
Exec ('Call [Members_Directory_Search_DEV].[dbo].[update_dirsearch_notes]
(?)',#v_constit_id_in) AT [DEV]
--Exec [DEV]..[Members_Directory_Search_DEV].[update_dirsearch_notes]
#v_constit_id_in
END
Error
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '.'.
Not sure what syntax you are used to using (CALL is used for Analysis Services, not SQL Server), but linked servers have a four part notation:
ServerName.DatabaseName.owner.object
Try:
Exec [DEV].[Members_Directory_Search_DEV].dbo.[update_dirsearch_notes] #v_constit_id_in
In SQL Server Management Studio 2008, I created a Stored Procedure, but its not appearing in the 'Stored Procedures' list when viewing in Object Explorer.
I've also restarted the server, and restarted my machine, and tested on other machines, and the Stored Procedure still isn't appearing.
I know that this Stored Procedure exists in the system because if I execute the following query:
exec dbo.sp_Orders
I get the following error:
Msg 201, Level 16, State 4, Procedure sp_Orders, Line 0
Procedure or Function 'sp_Orders' expects parameter '#OrderID', which was not supplied.
Therefore its recognising that the Stored Procedure exists, and even returns an error about a Parameter which was not supplied.
Why is this happening and how can I access the sproc?
you can try followign to access your sp:
exec sp_helptext xxxx where xxxx is your sp name.