|
In SQL Server 2005...
How to get Column Value From a Statement if Table Name passes dynamically through a variable....?
|
|
|
|
|
You need to use dynamic sql in your prodecure.
Declare @SQL varchar(100)
set @SQL = 'Select ' + @Field + ',' + @Field2 +' from TableName'
Exec (@SQL)
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Declare one more variable and assign first column value and Print this value.......
Okay..........
|
|
|
|
|
But why use a stored procedure at all? Just do the same thing but in regular code.
|
|
|
|
|
Hi I am using a SQL statement to access data from a database, if I hardcode a specific name in the field I get, the name being searched for is returned and displayed. Of-course this isn't a convient way of doing things so I introduced a text box to take in the data, store it in a variable then, this is were my problem lies, I try and used my variable in the SQL statement but it does not work. My code is as below:
<br />
static private string GetConnectionString()<br />
{<br />
<br />
return<br />
"Driver={Microsoft Access Driver (*.mdb)};"<br />
+ "Dbq=c:\\Users\\Tichaona\\Documents\\My Dropbox\\SET10101 Software Architecture\\coursework1\\patients.mdb;Uid=java;Pwd=password;";<br />
}<br />
<br />
private void BtnSearch_Click(object sender, EventArgs e)<br />
{<br />
String nameSearch = TxtBxName.Text; <<<The variable I wish to use<br />
try<br />
{ <br />
<br />
string connectionString = GetConnectionString();<br />
string queryString = "SELECT Name FROM patients WHERE Name LIKE '%nameSearch%';"; <<<Here<br />
using (OdbcConnection connection =<br />
new OdbcConnection(connectionString))<br />
{<br />
OdbcCommand command = connection.CreateCommand();<br />
command.CommandText = queryString;<br />
<br />
<br />
try<br />
{<br />
connection.Open();<br />
<br />
OdbcDataReader reader = command.ExecuteReader();<br />
<br />
<br />
<br />
while (reader.Read())<br />
{<br />
<br />
LstBxResults.Items.Add(reader.GetString(0));<br />
<br />
<br />
}<br />
<br />
reader.Close();<br />
}<br />
<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show(ex.Message);<br />
<br />
<br />
}<br />
}<br />
}
catch (Exception er)<br />
{<br />
MessageBox.Show("There is an error {0}" + er);<br />
}<br />
<br />
<br />
}<br />
<br />
<br />
}<br />
}<br />
<br />
<br />
|
|
|
|
|
Change the line
string queryString = "SELECT Name FROM patients WHERE Name LIKE '%nameSearch%';";
into
string queryString = "SELECT Name FROM patients WHERE Name LIKE '%" + nameSearch + "%'";
Use Code block instead of inline code for your code while posting here
[Edit]As Piebal's suggestion, Use parameters with command object Click here[^][/Edit]
thatraja |Chennai|India|
Brainbench certifications Down-votes are like kid's kisses don't reject it Do what you want quickly because the Doomsday on 2012
modified on Thursday, November 25, 2010 9:50 AM
|
|
|
|
|
Thanks it works great...
|
|
|
|
|
|
Negative several million!
Look up "SQL injection attack".
|
|
|
|
|
PIEBALDconsult wrote: Negative several million!
Look up "SQL injection attack".
Yes dude it's true, But he is just expecting just correct the error. Also here before I received a rude comment from Enquirer for the same type of suggestion. Ok Here after I'll add the additional details along with answer. Thanks
thatraja |Chennai|India|
Brainbench certifications Down-votes are like kid's kisses don't reject it Do what you want quickly because the Doomsday on 2012
|
|
|
|
|
Never ever ever etc. suggest concatenation. Not once, not even a little bit.
|
|
|
|
|
OK Deal
thatraja |Chennai|India|
Brainbench certifications Down-votes are like kid's kisses don't reject it Do what you want quickly because the Doomsday on 2012
|
|
|
|
|
According to question, Good Answer.
|
|
|
|
|
|
How to Get Value from Exec(Sql) in SQL Server 2005 if possible...? help me....
|
|
|
|
|
|
Use Print(sql) to have a full customized query.
Regards,
Hiren.
"We owe a lot to the Indians, who taught us how to count, without which no worthwhile scientific discovery could have been made." - Einstein
Microsoft Dynamics CRM
|
|
|
|
|
How can I declare varchar(MAX) in MySQL?
|
|
|
|
|
|
Hi All.
I have some small Access databases including identical tables.
For example let's suppose I got a.mdb b.mdb c.mdb with table1 table2 table3.
Is this possible to merge data from table1 table2 table3 in a SQL database without creating problems with ID colums of each table
(0;1;2;3;4......)
How this task can be realized?
Thank you in advance
|
|
|
|
|
KORCARI wrote: without creating problems with ID colums
What are you using for a datatype? If UniqueIdentifier, you should be OK; if auto-increment integers, you're hosed.
|
|
|
|
|
It's a terrible task.
In your small Access dbs, turn off the autoincrement feature of the ID columns. Then look for the max ID values in the first db, update the IDs in the second by adding that value, then take the new max value, ....
In SQL Server, use "SET IDENTITY_INSERT " + table + " ON" .
Now move your Access data to SQL Server, turn IDENTITY_INSERT off again, and throw away the small Access dbs.
|
|
|
|
|
KORCARI wrote: Is this possible to merge data from table1 table2 table3 in a SQL database without creating problems with ID colums of each table
Yes. Add a column called "sourceDatabaseName", make it part of the primary key, and when importing write the name of the database that you're importing into that column.
If you're moving a lot of data, then this would be the time to consider upgrading to Sql Server (express)
I are Troll
|
|
|
|
|
Hello
I want to connect my Vb.net application to SQL Azure database which is in cloud, what connection string i should use.
Thanks
Bhaskar
Senior Programmer
B R. Solutions, Delhi
www.brsolutions.info[^]
|
|
|
|
|
Connectionstring.com [^]is an excellent resource for this information
Never underestimate the power of human stupidity
RAH
|
|
|
|