I have 2 databases DB1, DB2 which have same table structure tbl_Detail. And both the databases have same stored procedure sp_GetDetails which has same scripts as below:
Create procedure sp_GetDetails
AS
Begin
select * from tbl_Detail
End
I need to move this from both these databases and place in a single database DB3.
I tried using linked server and passed an input param as dbname to the stored procedure.
But it requires to convert the script into dynamic SQL.
Is there any other way to achieve this, as I do not want to use dynamic SQL, since there are hundreds such stored procedures and the number of statements in each stored procedure are more.
Related
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.
I am trying to create the script in netezza like what we do in SQL server with variable declaration but I am not able to do it.
Need to create a temp table and then need to pass the parameter to it through a variable.
DECLARE var1 varchar(10);
through error message every time
How to drop a temp table. and how the temp table data is stored and cleared in the memory?
Netezza has its own language for stored procedures - same concept as SQLserver but closer to oracle syntax.
It is however not possible to use the 'variables' construct outside such a stored procedure, which leaves you with a couple of options in your case:
1. do a 'create or replace procedure' with your script embedded, and the execute the sp
2. store intermediate results in temporary tables, and do the 'if-then-else' logic in another scripting language (perl powershell or the like)
We went for option 1 in most cases when moving from SQLserver to Netezza about a year ago...
I query my database from within a .NET application (time recoding). I send the Windows user name to the database and query it depending on this information.
I query a lot of different information with stored procedures, such as holiday, overtime etc. and send them back to the .NET application where I show this data in one form.
Let's say I need the information from SP1, 3 and 4 on Form1:
FORM1:
SP1
SP3
SP4
My problem:
I have a quite complicated table that I need for these stored procedures. At the moment I create this table in every of those stored procedures as temporary tables what is obviously quite time consuming.
What I have tried so far is creating a stored procedure that creates a temp table but this one is not accessable within my other stored procedures.
My question: I am looking for a way to create this table for Form1 once, so I can just access these table in the other stored procedures.
I'm using SQL Server 2014 Express.
If you create the temporary table outside of any stored procedure, it will be accessible within each. Assuming that the actual table definition isn't too complex (but populating it may be), this may be doable.
So you'd execute (effectively):
CREATE TABLE #T (A int not null, B varchar(17) not null)
EXEC PopulateTempTable
EXEC SP1
EXEC SP3
EXEC SP4
All on a single connection. PopulateTempTable may or may not be required, depending on how complex the population of the table is and whether you want that to be performed by your calling code or the database.
You cannot create the temp table inside of a stored procedure since temp tables are automatically dropped when the scope of a stored procedure is exited.
Alternatively, you may want to simulate "session-global" temp tables, as I suggest in this answer1.
The risk with using a global temp table is that it is truly global - all sessions see and interact with the data in the same table. You have to be very careful in such circumstances if it's at all possible that two sessions will attempt to use it at the same time; You generally need some means to filter the data in the table so that each session only works with its own data.
1Probably enhanced these days by also adding a Logon Trigger to clear down the old contents of the table whenever a new connection is established.
You have to create global temp table
CREATE TABLE ##TEMP1
(
Name varchar(50)
)
I have a stored procedure that has boolean result parameter as output. But my project needs to use 3 databases. Basically, there is a main database and 2 others. The other databases using same stored procedure but they just depend on params. If I explain the scenario, you will understand. Firstly, Sorry for bad explaining.
This application using main database for session management, user configurations and so on. This is okay. The problem is same user has to use 2 different databases for creating invoices. We can pretend that these users are IT support staff. They works for 2 different companies and supporting their products and they are managing their solutions in different databases.
User
Main Database
A Company Data
B Company Data
Users have to create their invoices for each company's customers. Basically they are using same database but the name of databases are totally different. Maybe my problem has easier solution so I want to ask that How can I use Dynamic Database Name in my Stored Procedure? and my current question is I'm initialising Stored Procedure as String and after that I execute the stored procedure as String.
Procedure has no error, also says me completed successfully but there is no insert (in stored procedure). When I use the sql command (which I set manually as String) in Management Studio, Query is running perfectly.
As a summary,
I have 3 databases, Main database need to execute generating Invoice stored procedure. But database names are must be dynamic because of different companies.
When I send the database name A_COMPANY_DB, stored procedure should execute in A_COMPANY_DB. When I send B_COMPANY_DB, stored procedure should execute B_COMPANY_DB.
Both of A_COMPANY and B_COMPANY databases are same. I have to manage sql query as String because of Dynamic Database Name. So I can't reach the output parameter.
My stored procedure has only one output parameter which is bit type. But I can't use it like:
Set #dynsql = 'USE ' + QUOTENAME(#DbName) + ' exec.[dbo].[spName] ' + other params
EXECUTE sp_executesql #dynsql
In this situation I couldn't reach Output parameter. How can I set or use my Output parameter in this stored procedure?
EXECUTE sp_executesql #dynsql #outputparam OUT
Is it possible or any solution?
Sorry for bad explaining.
How do I set the database name dynamically in a SQL Server stored procedure?
Sometimes, the use of SYNONYMs is a good strategy:
CREATE SYNONYM [schema.]name FOR [[[linkedserver.]database.]schema.]name
Then, refer to the object by its synonym in your stored procedure.
Altering where the synonym points IS a matter of dynamic SQL, but then your main stored procedures can be totally dynamic SQL-free. Create a table to manage all the objects you need to reference, and a stored procedure that switches all the desired synonyms to the right context.
This functionality is only available in SQL Server 2005 and up.
This method will NOT be suitable for frequent switching or for situations where different connections need to use different databases. I use it for a database that occasionally moves around between servers (it can run in the prod database or on the replication database and they have different names). After restoring the database to its new home, I run my switcheroo SP on it and everything is working in about 8 seconds.
Stored Procedures are database specific. If you want to access data from another database dynamically, you are going to have to create dynamic SQL and execute it.
Declare #strSQL VarChar (MAX)
Declare #DatabaseNameParameter VarChar (100) = 'MyOtherDB'
SET #strSQL = 'SELECT * FROM ' + #DatabaseNameParameter + '.Schema.TableName'
You can use if clauses to set the #DatabaseNameParameter to the DB of your liking.
Execute the statement to get your results.
This is not dynamic SQL and works for stored procs
Declare #ThreePartName varchar (1000)
Declare #DatabaseNameParameter varchar (100)
SET #DatabaseNameParameter = 'MyOtherDB'
SET #ThreePartName = #DatabaseNameParameter + '.Schema.MyOtherSP'
EXEC #ThreePartName #p1, #p2... --Look! No brackets