We have a sp in which we have created local temp table and this SP is used in the worker process (micro service developed in the c#). This process call this SP. In a millisecond it makes multiple hit.
Let’s replicating this issue
We are creating same temp table in two
query windows.
IF Object_id('tempdb..#t', 'U') IS NOT NULL |
When we run this script in first window the temp table is created successfully.
When we are running this same code in other window we will get the below error.
The reason behind it: we can’t create two constraints
with the same name. In the above
scenario we are creating two temp table but we are trying to create the Primary
key constrain with the same name (Pk_id). Due to this we are getting this
error.
·
Create temp table without named constraint
·
Use table variable if we are getting less than
1000 records.
See here we are creating the temp
table without named constrain.
IF Object_id('tempdb..#t', 'U') IS NOT NULL |
Running this script in the first window.
Running the same script into the other window
Now we are not getting the error.
See the example of the variable table.
DECLARE @t TABLE |
Running the code in the first window
Running this code in the second window
Now we are not getting the error.
No comments:
Post a Comment
If you have any doubt, please let me know.