.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> Sql Server >> Post New QuestionBookmark and Share Subscribe to Forum

Table name as parameter in SQL Stored Procedure

Posted By: jokertin92     Posted Date: April 25, 2010    Points:2   Category :Sql Server
Hi I've Created a Stored Procedure which has the table name as In parameter

Create PROC Tablename
@name varchar(50)
AS
Select * from @name

But, throws an exception that "Must declare the table variable "@name".

How can I achieve this...

Responses
Author: Nikhil Kumar             
Posted Date: April 25, 2010     Points: 5   

you just write as:
create procedure [dbo].[GetDataFromTable]
(
@tablename varchar(50)
)
as
begin
EXEC('Select * from '+@tablename)
end

this will give you whole data of that table which one name you passed in the parameter

Be helpful and enjoy life because it comes only onces. Give all the happiness to yours and remember it doesn't comes by money ...
Author: jokertin92             
Posted Date: April 25, 2010     Points: 5   

Thank you... It works fine...

Author: jokertin92             
Posted Date: April 25, 2010     Points: 5   

Hi Nikhil,
Your answer is working fine when I parse in MS SQL Server. But when I use in my Code, I am getting an error that "Incorret Syntax near Form" and moreover my parameter doesn't take a value. I mean in my code I have given the parameter like ("@name",logfrm.cmblocation.text), here when I debug I see the value of logfrm.cmblocation.text as "" (null). That must be taken from the login page. The location that I specify in my login page, has to be taken...

Author: Nikhil Kumar             
Posted Date: April 25, 2010     Points: 5   

Friend i think you are misspelling recheck your words...and "Incorrect Syntax near FORM "
this can be a error.
if another problem freely ask ...

Be helpful and enjoy life because it comes only onces. Give all the happiness to yours and remember it doesn't comes by money ...
Author: jokertin92             
Posted Date: April 26, 2010     Points: 5   

Yes Nikhil I am getting the Error "Incorrect syntax near 'From'". Help me...

Author: jokertin92             
Posted Date: April 26, 2010     Points: 5   

Hey friends.. Ya, I found the problem... All your suggestions are OK, I mean the stored procedure is working. But, the problem is my parameter doesn't take the value from my Login page. I need another idea, How can I access the form1 property in form 2. I created an object property like
public partial class Login : Form
{
SqlConnection con;
SqlDataAdapter da;
SqlCommand cmd;
DataTable dt;
private string _place;
public string place
{
get
{
return _place;
}
set
{
_place = cmbLocation.Text;
}
}
}
But, still logfrm.place takes "null" value. So, I made the cmblocation.text(my login text box) as public and I accessed in Form2, but again the same problem occurs...

How to deal this? give me any suggestion please...



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend