using stored procedure to connect to db in external server - sql

I have a stored procedure that updates a table taking the data from a source and I want to modify the stored procedure so that it takes data from a table in an external database and updates a table in the original server. Can this be achieved and how ?

See linked servers, it's treated just a synonym: http://msdn.microsoft.com/en-us/library/ff772782.aspx
Actually synonyms are treated like linked servers..

Related

Copy and rename the Stored Procedure by using SQL server query

Greetings for the day!!
My requirement is we have Stored procedures created with one ID in database now i need to copy the script of Stored procedure created with existing ID and rename it with New ID.
Is there any SQL server Query to do above process.
PS:i am aware about sp_helptext. looking for SQL script.
Thank you.

Accessing data from another database in stored procedure

Following is my schema detail:
DB_A : schema_1, schema_2, schema_3
DB_B : schema_3
some procedures in schema_3 access resources(table, view, sp) from schema_1 and schema_2.
All procedures in schema_3 are same on both the dbs. How do I access schema_1 from schema_3 for both the dbs.
Now I can hard code DB_A in my procedures but when I move code to client machine, it will create a problem since DB_A may not be same(one of the reason being client is miser and having QA, Dev and Prod on same machine).
Second option is getting DB_A name as a parameter, but it will make all the schema_3 SPs dynamic (as I did not get any method to access something like #DBName.schema_name.ResourceName).
Third option is creating linked servers, which again do not solve my problem because of same reason as first.
Any idea how to proceed, where I do not want my procedures to be dynamic just because 80% of them are straight.
Edit Start:
So I can restate it as I have multiple databases with a database having resources (table/view/schema) which needs to be shared and then having other databases (one or more) which have stored procedures which computes on data from shared database and self database.
Shared database name is not going to be constant on all the environments and I want to change them(environment specific). I have come out with a solution where I will be creating synonym for all the shared resources and all procedures will be using them, that way they are all referring to shared resources from first database.
For each installation I need to modify synonyms definition to reflect correct share database name. Is there any SYNONYM For Database Name, that way I will have way less synonyms to handle.
Well the best choice I found is as follows.
Create Synonym (independent database DB_B) for individual objects (in shared database DB_A) with same name in same schema. That way your existing procedures need not change, and will work as required. Synonym gives a good reference on this. I will soon be creating an app to ease creating synonyms for these kind of situations.
CREATE SYNONYM DB_B.schema_1.proc_1 FOR DB_A.schema_1.proc_1
You can run your procedure in DB_A and create a view from DB_A to DB_B:
create view dbo.vw_B_Schema_3
as
select *
from DB_B.dbo.Schema_3
You'd have to create three versions of the view (dev, QA, prod.) But the view will be the only difference: procedure definitions can remain identical.
If DB_A and DB_B are on same server, only sure you that the login have permission in two database.
Now, use [database].[schema].[object], when you use object of others database
eg: I have two database, ("helpdesk", "intranet")
from heldesk to intranet
create view dbo.users
as
select login, name, lastname
from intranet.dbo.user // [database].[schema].[object] user is a table in dbo schema from intranet database.
where status = 1
;

How To Get 'Create Table' script for a table or sp using vb6 with sql server 2000

I need the "create table... " statement for a particular table and stored procedure to recreate them in another database. Forget the backup and restore. I have to do it in vb6. Its the same thing you get when you copy the Table And paste it in query analyzer. Its sql server 2000
Edit: Now I Know it can be asked as 'How To Script Entire Database Through VB6'.
Take a look at this StackOverlow post which talks about the same. Although it is not VB6-specific, you should be able to apply this solution without a problem.
Essentially you create a stored procedure that will generate the CREATE TABLE statement for the given table. The stored procedure will examine the sysobjects table to build the SQL.
Your VB6 application can run the stored procedure on server 'A' fetching the CREATE TABLE SQL. Then connect to server 'B' and run that SQL to create the table on the other server.

Can many Users open a SP with a TempTable in it?

After a user is logged into his system and opens my progamm he can see data from a database depending on his WindowsUsername(I wrote a little stored procedure for that).
This Data comes from a query with a temporary Table (#temp). My question is now, if many users use this programm, after opening it they would all try to build the #temp Table within the stored procedure. Is that even possible?? Because if i try to build a tempTable with the same name the server gives me an error. Do i have to give dynamic TempTable names maybe according to the user who is logged in??? Or is there another better option?
MS-SQL Server
A local temp table (one #TableName) is per session/connection
Many users can not share a session/connection
So, a local temp table in a stored procedure is safe for may concurrent users
On the other hand you use a global temp table (##TableName) then it is visible to world+dog
From MSDN (my bold)
There are two types of temporary tables: local and global. Local temporary tables are visible only to their creators during the same connection to an instance of SQL Server as when the tables were first created or referenced. Local temporary tables are deleted after the user disconnects from the instance of SQL Server. Global temporary tables are visible to any user and any connection after they are created, and are deleted when all users that are referencing the table disconnect from the instance of SQL Server.
If it is a true temp table within a stored procedure (SP) then the temp table is created/only exists during the execution of the SP. If the sp is run multiple times by multiple users at the same time the temp table is created multiple times and stored in its own memory space for each user.
The results returned to each user will be unique and each copy of the temp table will not see the temp table from other sp calls.
If you are testing in a query window by creating the temp table twice then you will get an error.
To prove that the above works schedule the SP to run twice at the same time passing a different user id. You will get two different result sets.

fetch data from other server in create procedure

I have to create a procedure to fetch few values from database but I don't have access to write procedure on that server. So, I wanted to create that procedure on another server and fetch the values. I wanted to know if is it possible to create procedure to fetch values from any other server?.
Assume that in the target database you have read only access to fetch data. In that case you could
Create a database link from the database where you would be creating
stored procedure.
Read the data from target database using the database link (db link).
Once you are able to get the data from target database you could do any manipulation you would want to. For creating database link in sql server you could check this