Execute stored procedure from Linked Server - sql-server-2012

I have stored procedure stored in DB01 the name is [cleanup].[spList_DB_Table_By_Column] #column_Name = 'customer_code', the stored procedure will find all column matched with the parameter supplied. So I want to run it in different server. How can I do that without writing the same SP in each different server?

I have figured it out.
EXEC [ServerName].[DBName].[schemaName].[spList_DB_Table_By_Column] #column_Name = 'customer_code'

Related

Can I have the same stored procedure with two different name?

Bellow, I have a stored procedure named usp_customer
CREATE PROCEDURE usp_customer
AS
BEGIN
SELECT * FROM CUSTOMER
END
I want to give the same exact stored procedure a second name usp_cust1
Note: I am not looking to rename or to create a new stored procedure, I want both names to work
In the end, I could use either EXEC usp_customer or EXEC usp_cust1
Thanks
edit: changed sp_ to usp_
One stored procedure can call another:
CREATE PROCEDURE usp_cust1
AS
BEGIN
EXEC usp_customer;
END;
Note that in SQL Server, you should not use the "sp_" prefix for stored procedures. That is best reserved only for system stored procedures.

Update Stored procedure (T-SQL ) modification automatically?

I have two identical stored procedures with dynamic query.
Let's say
Stored procedure A
Stored procedure B
Both are in different databases. But they have the same code (Not Complete Identical.4-5 lines differ).
Is there a way to update any modification done in stored procedure A to stored procedure B automatically?
Otherwise I always need to copy and paste changes manually. It is an error-prone activity. Can anyone help me on this ?
You could do something like that:
In database A:
design your stored procedure in a way, that you have a parameter for
the database in which you want to do the work
In database B:
Create a synonym for the procedure in database A
Example:
--create procedure in database A
create procedure dbo.StoredProc
(
#dbname --or dbid if you want
)
as
begin
--create your sql command here, using dynamic sql maybe
declare #sqlcmd NVARCHAR(MAX)=N''
set #sqlcmd = 'SELECT * FROM ' + #dbname + '.dbo.AnyTable'
exec sp_executesql #sqlcmd
end
--create a synonym for this procedure in database b:
create synonym dbo.StoredProc FOR databaseA.dbo.StoredProc
--then you can call your procedure in Database A and B like this:
declare #dbname NVARCHAR(100) = DB_NAME()
exec dbo.StoredProc #dbname
so you have to maintain your code only once, and in database b you only have kind of a "link" to this procedure.
hope this helps :)
This is basically what SSDT was designed for, the idea is that you write your T-SQL and schema as CREATE statements, you build a "dacpac" and then you use sqlpackage.exe to deploy the dacpac to whatever database you want.
Doing it this way you have an overhead of the SSDT project but it fixes exactly your main problem with the existing method "It is an error-prone activity."
My blog post shows how to get an existing database into SSDT (in this case adventureworks but replace adventureworks with your database):
https://the.agilesql.club/Blog/Ed-Elliott/AdventureWorksCI-Step2-MDF-To-Dot-Sql
Ed

Execute dbo.sp_send_dbmail using vb.net

I want to execute dbo.sp_send_dbmail this stored procedure and I am getting following error so please anybody help me
Could not find stored procedure 'EXEC sp_send_dbmail'
The sp_send_dbmail stored procedure is installed in the msdb database. You must either execute sp_send_dbmail from the msdb database, or specify a three-part name for the stored procedure.

Dynamic Table from stored procedure

I'm trying to create a temp table from stored procedures, from this link
In the string he defines the sql server version. Our clients have different types of sql servers, from 2005 until 2012.
String: 'SQLNCLI', 'Server=(local)\SQL2008;Trusted_Connection=yes;','EXEC getBusinessLineHistory'
How can I use that command independently from sql server plataform
The OPENROWSET creates a dynamic link to a remote server.
http://technet.microsoft.com/en-us/library/ms190312.aspx
You can create a dynamic TSQL call to a dynamic link with changing parameters. Below is sample code. This can be converted into a store procedure with a #my_Server passed as a parameter.
Please note, this does not support multiple calls at the same time since only one table exists.
You can not use a local temp table since there might be a scoping issue with EXEC calling sp_executesql inside a stored procedure.
These are things you will need to research.
-- Set the server info
DECLARE #my_Server SYSNAME;
SET #my_Server = 'Server=(local)\SQL2008';
-- Clear the staging table
truncate table STAGE.dbo.MYTABLE;
-- Allow for dynamic server location
DECLARE #my_TSQL NVARCHAR(2048);
SET #my_TSQL =
'INSERT INTO STAGE.dbo.MYTABLE SELECT * FROM OPENROWSET(''SQLNCLI'',' + #my_TSQL +
';Trusted_Connection=yes;'', ''EXEC usp_My_Stored_Procedure'')';
-- Run the dynamic remote TSQL
exec sp_executesql #my_TSQL;

Procedure or function 'sp_***' expects parameter '#', which was not supplied

I am trying to execute a stored procedure but getting following error:
Procedure or function 'SP_DELETE_DESIGN_PARAMETERS' expects parameter '#DESIGN_ID', which was not supplied.
Below is my stored procedure.
I am trying to execute it in SQL need result in table.
CREATE PROCEDURE SP_DELETE_DESIGN_PARAMETERS
#DESIGN_ID INT
AS
BEGIN
Delete From Design_Parameters where Design_ID = #DESIGN_ID
END
exec SP_DELETE_DESIGN_PARAMETERS
I know I am missing some very easy points. I would appreciate any help into this.
I am using SQL Server 2008 R2 and VS2010 with VB.Net
Thanks :)
exec SP_DELETE_DESIGN_PARAMETERS #DESIGN_ID = 5
exec SP_DELETE_DESIGN_PARAMETERS 5
Read up on how to pass parameters to a stored procedure. You are asking for a parameter but not supplying it.
When you exec your stored procedure you need to pass it the #DESIGN_ID parameter it expects.
EXEC SP_DELETE_DESIGN_PARAMETERS 123
where 123 is the ID you want to pass to the stored procedure