Temporary Table vs. Table Variable - both don't work - sql-server

I use SQL Server and Linq-to-SQL in my application.
I have to do a lot of database stuff and after a few hours work I have a stored procedure ready to run.
Sadly I use temporary tables in it (#TempTable) so Linq-to-SQL will give me an error for my return type (read about this e.g. here: LINQ "The return types for the following stored procedures could not be detected" (NOT temp tables)).
The only working solution for me is to switch to a table variable (DECLARE #temptable table) - but for this I found out it is not possible to use dynamic queries (which I do around 60% of my whole stored procedure).
Any ideas?

i found a solution to my problem: I simply wrap my first Stored Procedure with temporary tables in a second Stored Procedure with a single select.
This is automaticly recognized with an auto genereated object in LinQ and I'm happy :)

Related

How to get the return of a sql server procedure on Pentaho?

I have a Pentaho transformation with three steps:
Table input. It works perfectly.
Execute SQL script, which I exec a sql server procedure. This procedure returns 4 tables, but I am not able to catch this three tables.
Text file output. Here I would like to save the contains of the before returned tables.
My main problem is that my partners have developed a procedure that returns some tables, when clearly this is a misunderstanding of the procedure concept. It should be a table valued function.
Despite this, I have to fix this someway, so, I am asking for advise. Anyone knows how to catch the tables?
Greetings.
I have tried this in the past and I believe it is not possible (although I can't seem to find the docs at the moment). Even if you could achieve this, how would you tell Pentaho what to do with each of the separate tables?
My solution at the time was to alter the proc so that only a single table was returned. Either by splitting the proc into several procs or combining the output into a single larger table with a parameter to split out in Pentaho.

Suppressing output from a stored procedure in SQL Server 2008 R2 and newer

I am currently working with a stored procedure that performs some background processes and then returns one of two results tables.
If it works ok I get a one column table that says success, if it doesn't then I get a four column table with various error data.
While this is fine if you just execute the code from .net, I now need to execute this from within another stored procedure. While I don't need the output, I do need the background processes to take place. I'd usually insert the output into a table, but can't in this case as the columns in the output varies dependent on the result, and as such cannot define a table that it can insert into.
Easiest answer would be to rewrite the outputs of the background SP to be consistent but this isn't an option. I've even tried wrapping this inside a UDF but the stored procedure can't be called from with a function.
Whatever solution I finally use it must work on versions from SQL Server 2008 R2 up to 2016.
Does anybody have any suggestions?
Many thanks,
Mat.
I would image you could create a SP that inserts the result of the inner SP into a temporary table using the hack below.
Insert results of a stored procedure into a temporary table
If that blocks the ouput then you can return no data.

SQL Server - compare the results of two stored procedures that output multiple tables

So, similar to "SQL Server compare results of two queries that should be identical", I need to compare the output of two stored procedures to ensure the new version is generating equivalent output to the old version. The tricky part is that my SP outputs six tables of differing widths.
I started writing a hybrid version of them that would compare each of the tables individually, but it's a pretty complex SP, so I was hoping there was an easier way.
I tried using EXCEPT as in the linked question, but it looks like that will only compare one table to one other table.
Easy option 1: Output the stored procedure results to a text file (one per procedure version) and use a diff tool/editor to make sure they are the same.
Easy option 2: Write the stored procedure results to a table/temp table (per return table per procedure) and write sql to compare the results. Just count the rows in each result table and then do a count of the union (not union all) of both tables. Repeat for each result table.
You can capture multiple result sets in .NET (C# or VB) quite easily. You can create a DataAdapter and DataSet, and use the DataAdapter.Fill() method to populate the DataSet. Each result set will be stored as a DataTable within that DataSet. Then you just need to loop through the DataTables collection in each DataSet and compare them. You can find more info on this MSDN page: Populating a DataSet from a DataAdapter
This can be done in either SQLCLR if you want to run it as a stored procedure or user-defined function, OR it can be a stand-alone console application. Running it as a SQLCLR stored procedure is quite convenient, but given that you will be stored all results for all 6 result sets, and for both stored procedures that you are testing, that might require too much memory. In that case, the console app is the the way to go.
The only thing I can think of is add an additional parameter to your both of (New/old) stored procedures to handle which result it should return like.
Exec usp_proc #var1 , #var2 , #ResultSet = 1
The above execution should return the first result set and if you pass #ResultSet = 2 it should return second result set and so on.....
do this with both stored procedure and then compare the result sets group by group (using except will do the trick).

SQL - How to check if altering a table will break any stored procedures

Is there a nice way before I alter a table (e.g. remove a column), to see if that this will break any stored procedures?
I am trying to do this in MS SQL Server
Use the query here to search all stored procedures for the table and column name. You will probably still want to look at the code for each one you find to verify that it will or won't break.
you can use the following query to search for the table name in any stored procedures:
SELECT name
FROM sys.procedures
WHERE Object_definition(object_id) LIKE '%Your_Table_Name%'
I suggest you:
Make sure you have a separate environment (DEV)
Use the sample code from here to create a proc that confirms all objects in the database can be recompiled
How to Check all stored procedure is ok in sql server?
Use it - I can guarantee you will already have failing objects before you remove your column
Remove your column and use it again to see if more things broke
The more mature approach to this is to put your database into a database project and build that. But you can't do this until your database is valid.

Mass change datatype and rename of dependent store procedure variables

I am in the process of optimising my database and I was thinking of changing the datatype for some columns from DATETIME to SMALLDATETIME on my tables.
Is there a system stored procedure that returns both the contents/code of a store procedure and the dependent table which will then allow me to do a join on a filtered list of tables?
Cheers!
EDIT1:
Im looking to programatically rename the stored procedures not track dependencies!
The built-in dependency tracking for SQL isn't very good for this type of work. Two tools come to mind thought...
Red Gate SQL Dependency Tracker - Good for determining all the dependent code
Visual Studio for Database Developers - Contains TSQL Code Analysis which can identify if a piece of data is being treated as an incorrect type.
Red Gate has a free trial on their stuff, which might get you through this job
I answered a simliar question to this (link below) with a sample of a scipt I use to find text in stored procedures (and functions and views). It requires a bit of work, but might help you here.
[How to find data table column reference in stored procedures
[1]: http://How to find data table column reference in stored procedures
If your dependencies in SQL Server are accurate, you can use sys.sql_dependencies with appropriate joins.

Resources