Click here to Skip to main content
15,922,630 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have storedprocedure .sql file, it contains content from "create proc ......". I want to run this dynamically.
I select .sql file from one location and read this and send content as an parameter in a storedprocedure. Now i want to run this content. What can i do? pls help me.:-)
Posted
Comments
[no name] 6-Dec-11 3:13am    
Please explain further. We don't get it

What i have understood from your question the file in which you have saved the sql stored you are trying to access that one.

First thing you should understood that the stored procedure saved in the file is just for the user and not for the sql server. The actual stored procedure is saved as database object in your database.

you just need to create an sql connection object and command object which are going to access your stored procedure from the database.

You can check this code:

public void RunStoredProc()
	{
		SqlConnection conn = null;
		SqlDataReader rdr  = null;

		Console.WriteLine("\nTop 10 Most Expensive Products:\n");

		try
		{
			// create and open a connection object
			conn = new 
				SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI");
			conn.Open();

			// 1. create a command object identifying
			// the stored procedure
			SqlCommand cmd  = new SqlCommand(
				"Ten Most Expensive Products", conn);

			// 2. set the command object so it knows
			// to execute a stored procedure
			cmd.CommandType = CommandType.StoredProcedure;

			// execute the command
			rdr = cmd.ExecuteReader();

			// iterate through results, printing each to console
			while (rdr.Read())
			{
				Console.WriteLine(
					"Product: {0,-25} Price: ${1,6:####.00}",
					rdr["TenMostExpensiveProducts"],
					rdr["UnitPrice"]);
			}
		}
		finally
		{
			if (conn != null)
			{
				conn.Close();
			}
			if (rdr != null)
			{
				rdr.Close();
			}
		}
	}
 
Share this answer
 
Comments
[no name] 6-Dec-11 4:07am    
Hi,

I made one sp like as:
Create proc sarvesh
as
Begin
Select 0
end

I want to run this script at client end database from my end. I send this as an parameter in string in other sp.
Can you tell me, how can i run this?
If you want to run an SQL statement dynamically inside a stored procedure, you can use sp_executesql[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900