Default value in table column based upon a stored procedure - sql-server

I have a table in SQL Server 2008 (MyTable) which contains a field (MyField). MyField cannot take nulls and it will be populated with a default value. The default value is calculated by means of a complex stored procedure (MyStoreProcedure), which invokes other tables and fields in my database. My users should have the option to change this default value once it has been supplied by SQL server. SQL server, however, will have a AFTER UPDATE TRIGGER to check that the entered value in MyField conforms to certain rules.
I have tried to populate MyField with the intended default value by means of a 'TRIGGER AFTER INSERT' and SQL Server tells me that null values cannot be inserted into MyField, which is actually true. Is there a workaround to implement this default value based upon a stored proc?
Thanks

This may be a case for an INSTEAD OF INSERT trigger, which will allow you to access the stored procedure and set a default value before the row(s) is/are inserted into the target table.
The trick is then getting the stored procedure result(s) into the trigger. One approach would be to insert the stored procedure results into a temporary table, which then may be joined with the INSERTED meta table for the actual insert. See How to SELECT FROM stored procedure.

Related

Use Stored Procedure as Default Value of a column

I'm trying to a use stored procedure as a default value of a column. It's called "dbo.GetAdmin"
Basically, this stored procedure looks up for the user id "admin" and if it doesn't exist, the SP inserts it then returns it as a user record.
Now I want to use this SP as a default value for a column like this :
ALTER TABLE [dbo].[Transaction] ADD CONSTRAINT [DF_Transaction_From] DEFAULT ([dbo].[GetAdmin]) FOR [From]
But I get this error :
The name "dbo.GetAdmin" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
In this related post, an user told us that we have to use a function instead of a stored procedure : https://stackoverflow.com/a/2851783/5591761
Because user-defined functions are allowed as a default value for a column.
But according to the documentation of a function :
Functions are computed values and cannot perform permanent environmental changes to SQL Server (i.e., no INSERT or UPDATE statements allowed).
So, because I have to insert the user if it doesn't exist, I have to use a stored procedure instead of a function. How can I workaround this issue ?
An insert trigger would allow this if it had to be done. I'm assuming the Admin value does not change per record in the insert should somebody insert more than one record. Otherwise, a loop would be required to execute the procedure for each record. I would think hard about alternatives before doing this. It might be that this has to be done for consistency. Depending upon what the procedure is doing, it might be possible to do a set based query in the trigger by pulling the proc code into the trigger.
For example, trigger code might be something like
SET NOCOUNT ON
IF NOT EXISTS(SELECT * FROM inserted) RETURN -- no inserted records (where clause)
DECLARE #AdminId int -- the same value for all records inserted by statement
EXECUTE #AdminId = [dbo].[GetAdmin]
IF #AdminId IS NULL RETURN -- don't bother updating NULL to NULL
UPDATE t
SET [From] = #AdminId
FROM [dbo].[Transaction] t
INNER JOIN inserted i.record_key = t.record_key -- match each inserted to itself uniquely
WHERE i.[From] IS NULL -- no value was provided upon insert
RETURN
Also, error handling can be tricky calling a procedure from a trigger.
If you DBA can insert a record upon request in SSMS without supplying the ADMIN value, then you might not need this.

Syntax for creating temp table with their unique name by select query in stored procedure using SQL Server

I want to create temp table with their unique name by a select query in a stored procedure in SQL Server.
For example: whenever I run the select query at that time different temp table name want to create.
Let be more clear, at the first time when I will run the select query at that time temptable name is temptable1, while at the second time the table name will be temptable2 and so on.
I want to know the syntax for executing the select query and creating the temptable with their unique name in a stored procedure in SQL Server.
In the context of the SQL Server Stored Procedure, the engine is handling itself the names of the temporary tables.
There is no need to worry if many users are executing the same stored procedure in same time - the temporary objects cannot be shared across them and no conflicts are going to happen.
Also, naming a temporary table in stored procedure with different name can be done using a dynamic T-SQL statement. You can for example, use a sequence to get such number and concatenate it to the table name. But, if you do so, you need to use sp_executesql to create your table and do things with it. In such way, no other stored procedure would be able to read/modify the table you have created in the current stored procedure. In other words, the temporary table cannot be shared over the routines if created using dynamic T-SQL statement. So, there is absolutely no point of doing such thing.

SQL Server 2012 Stored Procedure to accept an IN parameter and do multiple inserts

I want to create a SQL Server 2012 Stored Procedure that will take an array from PHP as an IN parameter.
The table that the procedure will insert into is called dbo.Users and it will have three columns that I care about: createdby, userid, emailaddress
The array will include userid and email address. It call will include one OR more records to insert into the Users table.
How do I get the procedure to accept an array and how do I turn the array into multiple insert statements into my Users table? Do I need to use table value parameters? I heard of them but haven't used one before.
Thank you!
You can use XML parameter to pass a set / list(s) of related data to a stored procedure, or you can use Table valued parameter.

Insert data from stored procedure returning mutiple dataset into a temporary table in SQL Server 2008

My stored procedure returns more than one result set.
I want to get the data returned by the second select statement from the stored procedure and I want to insert that data in a temporary table in SQL Server 2008.
Any idea how to implement this ?
You can't access more than one result set in SQL Server. A couple of options though:
Split the stored procedures so only one result set it returned.
Access the second result set using c#/vb/net data tables.
Create a CLR to access the second result set.
HTH
C

SQL server write once column

Is there any way to configure a SQL server instance to not permit updates to values inserted in the database?
e.g. once inserted, the value is fixed, other columns in that same row may be changed, but that value is only written once.
Write a trigger on update that checks the current column against the new value being inserted and rolls back the transaction if the values differ.
create trigger dbo.tr_no_updates
on mytable
for update
as
if update(mycolumn)
rollback transaction

Resources