I have a very strange situation that I have tried to research the answer from the Net to no avail. I am using SQL Server 2008 R2.
I have created a stored procedure that has a TRUNCATE TABLE statement on it amongst other T-SQL statements.
When this is run from SQL Server Management Studio, the stored procedure runs without error.
When run from a Windows Forms application (written in VB.Net with .NET Framework 4), I am getting the error
Cannot find the object tbl_Test1 because it does not exist or you do
not have permissions
The connection to the database is set within the application correctly, and what is even stranger is that I have other stored procedures created in exactly the same way, with their own TRUNCATE TABLE statements and these still run without error.
There is execute permission on the stored procedure to the User_Role, of which the calling application is logging on as.
The table tbl_Test1 DOES exist.
I have tried several things and from doing this, have become even more confused about this whole situation.
If I put a Select * from tbl_Test1 prior to the TRUNCATE, then the stored procedure works.
Because I am returning a select later on, I would prefer this not be present. So I recoded and did a select #Count = count(*) from tbl_Test1 before the TRUNCATE statement, and this fails with the same error above stating the tbl_Test1 doesn't exist or no permission.
I am at a complete loss as to why this might occur. I have several other stored procedures that have truncate statements within them, created in exactly the same way as this one and those work fine when called from the application.
Can anyone help or shed some light on my problem.
Many thanks in advance
Ownership chaining doesn't apply to TRUNCATE TABLE.
You need to grant that permission explicitly (the minimum permission required is ALTER on table_name) or use EXECUTE AS in the stored procedure. Giving EXEC permissions on the stored procedure is not enough.
Related
I have a stored procedure that runs successfully in SQL Server that is not loading anything into the body of the report. There are only 4 parameters to this procedure, 2 varchars and 2 ints.
I have verified the following:
The stored procedure works in SQL Server
The stored procedure works in SSRS when using Query Designer
The stored procedure shows as being executed and completed in SQL Server Profiler
Running what is shown in the SQL Server Profiler does return the expected results
There is no filtering on the table containing the results
There is no visibility restrictions on the table containing the results
The connection to the database is successful using "Test connection" as mentioned here
I have added a second different dataset into the report and it is showing in the body with no problems.
I have recompiled the procedure as mentioned here and it is still not showing in the report.
There are no multi valued parameters, so this answer is mostly irrelevant.
We are not using Visual Studio, so there is no .DATA files like mentioned here
However for the life of me, I can not get anything from this stored procedure to show up in the body of the report.
Due to legal reasons, the stored procedure cannot be posted in this question as it was part of a software our company is using. We have permission from them to use the procedure to make the SSRS Report for our needs.
Further diagnostics and logging are always useful.
You could have the procedure log additional data to a dedicated table for review later.
Useful data would be that which is related to the user's session based on ##spid and the system views sys.dm_exec_sessions, sys.dm_exec_requests, sys.dm_exec_input_buffer() such as statement start time, any blocking spids, wait_types. Also check the connection level properties are consistent and as expected eg quoted_identifier, ansi-settings etc.
You could also log information about the executing queries such as Rows Affected ##rowcount and the values of any parameters or intermediate values.
I have an oracle function which needs to call a SQL Server Stored Procedure.
I created a database link in oracle to the SQL Server.
I'm using DBMS_HS_PASSTHROUGH to call the stored procedure and it actually calls it. The stored procedure has some logic in it. There are some select statements in the Stored Procedure and those actually work, but it also contains some update/insert/delete statements and those do not work.
When I commented out all the update/insert/delete, I get a successful response from the SQL Server executing the Stored Procedure back to Oracle. When those updates and inserts are to be performed, I get
"ORA-00942: table or view does not exist"
Like I said I'm able to execute stored procedure, also a nested stored procedure gets executed, functions work as well. The problem is when update/insert/delete are to be performed. I checked the permission on the table and all seem to be correct.
For example, if I Deny execution of stored procedure for the DBLINK user, I will get a response Access Denied. But if I Deny Select Statements for the DBLINK for that table, those Select Statements will still get executed. It seems like if once we are in the Stored Procedure, the user executing the select/update/insert/delete is not longer "DBLINK".
any help would be appreciated!
I'm not expert on this, so if you guys know of a better way to call a stored procedure from oracle, please let me know.
My database is Azure Data Warehouse. My Python version is Python 2.7 and I am using PyODBC to call SQL statements and stored procedures against the database.
The script can connect and does execute stored procs and SQL fine against the server. However, I have one stored proc with no parameters that is simply doing something similar to below from an external table (Azure BLOB):
TRUNCATE TABLE dbo.Table1;
TRUNCATE TABLE dbo.Table2;
TRUNCATE TABLE dbo.Table3;
INSERT INTO dbo.Table1
SELECT * FROM ext.Table1;
INSERT INTO dbo.Table2
SELECT * FROM ext.Table2;
Etc...
All Truncate statements succeed. Inserts work on first table. The script ends without doing inserts on the other tables. Running the stored proc with the same user in SSMS shows it works fine. Yet, PyODBC does not seem to complete the stored proc without any errors or warnings. It looks to work, but doesn't.
The only thing I can think of is the fact this is off External Tables and the External Tables do have dirty reads that are sent to the client as rejected messages. PyODBC may be failing silently because of the rejected messages returned back to the API.
I have a stored procedure witch runs several EXEC commands. As a result it returns more than one table. In SQL Server Report Builder or SQL Server Data Tools (SSDT) I can only access the first table it retrieves from this stored procedure. But I need to access last table, in which contains the merged columns from different tables produced by different stored procedures.
I have tried the hide tables other than the last table, but failed. Is there any suggestions you can offer to solve this problem. I appreciate and thank with all my hearth to whom tries to contribute the solution of my problem.
I found a solution for this problem. It is not quite what I have asked but solves this issiue. Here is the solution:
I have edited all sub-stored procedures that I used to "RETURN 0" to prevent them to give an output. So when I call them from Main stored procedure they have no visible output at the "Results" window. Only the Main stored procedure has a single output. Thus I can use it in Report Builder or SSDT like a normal stored procedure without any more modification.
I'm using SQL Server 2008 Enterprise. I created a procedure in one database. The procedure is composed of several queries to different databases and the final combined result set is being displayed.
I try to execute it via Excel, so the results will appear automatically in Excel sheet, but I'm getting the error:
The query did not run, or the database table could not be opened. Check the database server or contact your DBA. Make sure the external database is available and hasn't been moved or recognized, then try the operation again
I created a simpler procedure that queries only one database, and the results displayed at the Excel sheet with no issues.
Hence I suspect that, the original procedure failed due to the fact that I'm querying several databases in the procedure, when in the connection details of the "External Data Properties", only one database is mentioned.
My question is - can it be solved? Can I use multiple databases in the procedure and see it in the Excel?
Thanks,
Roni
I transformed the procedure to have Table Variables instead of Temporary tables and I've added "set nocount on" to the beginning of the procedure.
The second action solved the issue.
The first action improved the response time of the procedure.
(Copying key parts of #Roni's answer)
create procedure dbo.xxx
as
set nocount on
...