SQL Server 2017 Doesn't Validate User Function when updating Temp table - sql

This is a crazy one, seems like a bug to me. I changed the signature of a scalar user function to include an additional parameter, and I was trying to refactor my sprocs which call it, and I noticed an issue which I was able to recreate with two test functions.
First, I created a scalar user function called "test_function" which take a single integer parameter. It doesn't matter what the function does for this test.
Then I created a sproc, which has a simple temp table of 1 column value "thevalue"
Then I execute the alter or create update, but without the parameter:
Intellisense correctly indicates that there's a parameter missing in test_function. But here's where the problem occurs, I can still create or alter this sproc without any errors!
However, if I remove the temp table from the equation, and update a variable, I get the expect error when trying to create or alter the sproc:
Is there a possible setting or a way to flag this specific sproc so that the error would occur and the sproc not be altered?

Related

Variable in Stored Procedure

When I execute one of my stored procedures manually, I have to populate several variables.
Most of the variables don't change each time it is run; is it possible to pre-populate the "Value" box so that it only needs to be changed when necessary?
I am reluctant to hard code in the script as there was a series of interlinked procedures which I need to keep dynamic
I'm going to go out on a limb here and guess that you're talking about SQL Server, and that you're executing your procedure through SSMS, because of your description of the graphical interface. In the future, please tag your question with the specific database platform that the question pertains to, and try to be responsive to early comments. You'll get answers much, much faster. (If I'm wrong, just undo the tagging I added to your question.)
Although stored procedures can contain variables, what you're talking about here are parameters; values that are passed into the procedure from the calling code or application.
Parameters can be defined with default values in their declarations.
CREATE OR ALTER PROCEDURE dbo.SomeProc (
#SomeBigIntegerValue bigint = 42
)
AS...
When default values exist, the parameter becomes optional for the caller. The procedure can now be called with or without explicit parameters. Either of these will run.
EXECUTE dbo.SomeProc;
EXECUTE dbo.SomeProc
#SomeBigIntegerValue = 37;
In the first instance, the procedure will use the default value, 42. In the second instance, it will use the parameter value, 37.
You'll note that I named the parameter in the call. That's a best practice, generally, to avoid confusion, but it also allows you to send the parameters in any order. If you don't name them, they will be interpreted in the order they're declared, so you run all manner of risks there.
If you choose to execute the procedure through the GUI, the default values won't be pre-populated, but you can see which parameters have defaults and which don't by expanding the Parameters tab under the procedure name in SSMS. I couldn't find an example with defaults, but it'll looks something like this:
If you want the procedure to use the default value, just tick the Pass Null Value check box.
(In case you're wondering, we have a truncate proc so that our ETL service accounts can have scaled back permissions without having to do fully-logged, row-by-row deletions...)

How to tell if ALTER PROCEDURE worked?

I'm pretty new to SQL and SQL Server. I'm trying to run an ALTER PROCEDURE query from a .sql file called through C# code. Before I move on to making sure my query does what it's supposed to do, I want to verify that my ALTER PROCEDURE query actually altered the procedure, but I don't know how to verify that.
For example, in SQL Server, I can see where the stored procedure I'm trying to edit lives, in:
- database-name
- Programmability/
- Stored Procedures/
- dbo.MyStoredProcedure
If my ALTER TABLE query worked correctly, would I be able to see my procedure code here, or would I check somewhere else? Or am I thinking about this the wrong way?
Generally, we rely on error and exception messages to tell us when something like this has not worked. However, I suppose that it might be possible that the procedure Alter-ed was not the one that was intended (implying bugs in the name/path/call construction, of course).
In that case, you can get the current text of any SQL Module (Procedure, View, Trigger, etc., anything script-baseD) from the sys.sql_modules table:
SELECT definition FROM sys.sql_modules
WHERE object_id=OBJECT_ID('dbo.UserSamples_Insert')
You should note that usually when something like this happens without an error message it is because either:
You are executing in the wrong database (like PROD when you meant to be in DEV or vice-versa), or
You are not using the correct Schema (because you can make and use schemas other than 'dbo').
Wait, you say ALTER PROCEDURE twice, but then the third time you say ALTER TABLE. Which is it? I ask because unlike almost every other SQL object, tables are not script-based and their definition cannot be found in any of the Sql script repositories like sys.sql_modules. I actually use either SMO (from a client) or a tool that #SeanLange wrote years ago for that (from the server itself).

What can I do instead execute stored procedure in sql server function

In a select statement I am calling a function which I should show result of it in the result table of select statement. In this function, I need to recreate (it is just checking actually if it is necessary to be recreated or not then it will recreate if necessary) a table which the function uses and I know I can not do that in a function so I created a stored procedure to recreate the table.
I can not exec stored procedure before the select statement because I need to do that in every row. Because the content of the table may need to be changed according to parameters of it every time.
But I know that I also can not exec a stored procedure within a function because of this error Only functions and some extended stored procedures can be executed from within a function. So in final situation, I need to exec stored procedure in a function in order to recreate the table or I need to create a non-temporary table in a function. I could not find any solution for this. Is there any solution to do that in a different way?
I am trying to explain what am I trying to do in details. The table that I mentioned above, stores a list of start times and end times of some workers. In the function I am using this time informations and doing something with it.
But In my select statement I am sending a time parameter to the funciton and this time can be old, for example a month ago. And start and end hours of workers can be different at a month ago. So in this situation according to time parameter of the function I may need to recreate the table. But I can not do that in a function so I did it in a stored procedure but I can not exec this stored procedure in a function as well. And I also can not create a table in the function.

Is there a way to drop a User Defined Table type that is already bound to a stored procedure?

I have an user defined table type (UDTT) that is already bound with a stored procedure which uses it as a input parameter. I have made a mistake in UDTT structure but I cannot alter UDTT so when I tried to delete it, SSMS complaints that its bound to store procedure so it cannot be deleted. Is there away to delete my UDTT?
thanks
It is required on the drop, and what I normally do is set myself a template script that follows this execution:
-- Table builds or the type
if object_id(N'dbo.<tablename>') is null
begin <create table chunk> end
-- Proc Drops
-- UDTT Drops
-- UDTT Adds
-- Proc Adds
Then if you ever need to Alter a proc, you just seek it on that script and execute the script again. Since there is no saved data in a procedure, this solves type overlooks as well as recompile options needing processed. I found it to make development as well as testing much more conducive.
There is an easy work around here. I use SSMS, and select
SCRIPT AS > CREATE TO > NEW QUERY EDITOR WINDOW.
This gives a script to create. I then rename the existing data type rather than deleting it. Make whatever changes are needed to the recreate script, and execute.
You can then drop the renamed data type.
Its a bit of a work around, and an alter command would be better, but this does the job.

User define function with in stored procedure

can we create user define function with in stored procedure then end of the store procedure we need to delete that custom user define function.
You can but it could get messy.
Look at sp_executesql. This will allow you to run arbitrary SQL, including DDL. Creating and using UDF's in this way does seem a bit dangerous -- you'll need to make sure that there aren't any name conflicts with competing threads, and there's no way to get any kind of query optimization.
I'd double check your design to make sure there isn't another solution to this!
Dynamic SQL is the only way.
ALTER PROC ...
AS
...
EXEC ('CREATE FUNCTION tempFunc...')
...
EXEC ('DROP FUNCTION tempFunc')
...
GO
However:
if you have 2 concurrent executions it will fail because tempFunc already exists
if each udf definition is different, then you need random names
if you randomise the name, the rest of the code will have to be dynamic SQL too
a stored proc implies reuse so just persist it
your code will need ddl_admin or db_owner rights to create the udf
...
So... why do you want to do this?