How to pass multiple command in one query? - sql

Hi all how can I write code below more simply? ... so two sql(OleDb) command in one query.
con.open();
cmd.CommandText = "DELETE * FROM Customers WHERE ID_C = 1";
cmd.ExecuteNonQuery();
cmd.Clone();
cmd.CommandText = "DELETE * FROM Books WHERE ID_B = 1";
cmd.ExecuteNonQuery();
cmd.Clone();
con.close();

Provided that you don't need the number of rows affected (returned by ExecuteNonQuery) you can simply put both commands in the same SQL separated by ";"
con.open();
cmd.CommandText = "DELETE * FROM Customers WHERE ID_C = 1; DELETE * FROM Books WHERE ID_B = 1";
cmd.ExecuteNonQuery();
con.close();

Related

SQL query in Excel table

I am running a query using OleDbCommand in a database in Excel, containing the following columns:
name, city, inhabitants
My code:
Cmd = new OleDbCommand();
Cmd.Connection = Conn;
Cmd.Parameters.AddWithValue("#city", city);
Cmd.CommandText = "Select City, count(habitants) as h from [Sheet1$] where city = #city group by city";
var Reader = await Cmd.ExecuteReaderAsync();
I get this error:
OleDbException: You tried to execute a query that does not include the specified expression 'city' as part of an aggregate function.
Why does this error appear if it contains the city column?
You can probably work around this error by adding GROUP BY to your query:
Cmd.CommandText = "Select City, count(habitants) as h from [Sheet1$] where city=#city group by City";
Use the FIRST() function for the city:
Cmd.CommandText = "SELECT FIRST(city) AS city, COUNT(habitants) AS h FROM [Sheet1$] WHERE city=#city GROUP BY city";

ORA-00933: oracle 8i

I want to use join in Oracle 8i. I have my query as below.
I have this query of getting data from two tables using an join, but I get the error SQL command not properly ended.
private List<StamfordProdRelease> GetStamfordProdReleases()
{
List<StamfordProdRelease> list = null;
string srtQry = "SELECT NVL(NULL, 0) ID," +
" DLOG.RELEASEID AS RELEASE_BUILD," +
" TRUNC (DLOGDET.DEPLOYDATE) AS PROD_DEPLOY_DATE," +
" DLOGDET.DEPLOYREQUEST AS BAAR_RFD," +
" DLOG.FILENAMEEXT_VC AS SCRIPT_NAME," +
" DLOG.VERSION," +
" DLOG.REQUEST," +
" DLOG.NOTE AS COMMENTS" +
" FROM ADM_DEPLOYMENTLOGDETAIL DLOGDET" +
" JOIN ADM_DEPLOYMENTLOG DLOG ON DLOG.LOGNO = DLOGDET.LOGNO;";
using (OracleConnection conn = new OracleConnection(Globals.Constants.AppConnectionStringReadOnly))
{
using (OracleCommand objCommand = new OracleCommand(srtQry, conn))
{
objCommand.CommandType = CommandType.Text;
DataTable dt = new DataTable();
OracleDataAdapter adp = new OracleDataAdapter(objCommand);
conn.Open();
adp.Fill(dt);
if (dt != null)
{
list = ConvertToStamfordProdRelease(dt).ToList();
}
}
}
return list;
}
My target is to insert records into a table.
Keep everything in one set of " and also you only need a single ; to end the SQL query outside of the double quotes.
private List<StamfordProdRelease> GetStamfordProdReleases()
{
List<StamfordProdRelease> list = null;
string srtQry = "SELECT NVL(NULL, 0) ID,
DLOG.RELEASEID AS RELEASE_BUILD,
TRUNC (DLOGDET.DEPLOYDATE) AS PROD_DEPLOY_DATE,
DLOGDET.DEPLOYREQUEST AS BAAR_RFD,
DLOG.FILENAMEEXT_VC AS SCRIPT_NAME,
DLOG.VERSION,
DLOG.REQUEST,
DLOG.NOTE AS COMMENTS
FROM ADM_DEPLOYMENTLOGDETAIL DLOGDET
JOIN ADM_DEPLOYMENTLOG DLOG ON DLOG.LOGNO = DLOGDET.LOGNO";
using (OracleConnection conn = new OracleConnection(Globals.Constants.AppConnectionStringReadOnly))
{
using (OracleCommand objCommand = new OracleCommand(srtQry, conn))
{
objCommand.CommandType = CommandType.Text;
DataTable dt = new DataTable();
OracleDataAdapter adp = new OracleDataAdapter(objCommand);
conn.Open();
adp.Fill(dt);
if (dt != null)
{
list = ConvertToStamfordProdRelease(dt).ToList();
}
}
}
return list;
}
Oracle 8i did not support standard ANSI SQL JOIN syntax.
That feature was introduced in Oracle 9i Release 2 (aka Oracle 9.2)
Quote from the chapter "What's New in SQL Reference"
SELECT [...] has new ANSI-compliant join syntax.
Don't combine the, strings put all in one.

Writing SQL statement to select from multiple rows

This might be a silly question to ask. I will appreciate any kind of help.
I'm trying to write a query that count the number of rows based on the logic as below:
count no rows where username = #username and friendsname = #friendsname and username = #friendsname and #friendsname = #username where status = 0
consider the table in the diagram below:
The query should return 1 since id '18' and id '20' have reciprocal values. How will this query be written?
I have written the query as follows which doesn't work:
bool flag = false;
StringBuilder query = new StringBuilder();
query.Append("SELECT Count(id) from friends where username=#username AND friend_username=#friend_username AND username = #friend_username AND friend_username = #username");
query.Append(" AND status=0");
using (SqlConnection con = new SqlConnection(Config.ConnectionString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand(query.ToString(), con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("#username", username));
cmd.Parameters.Add(new SqlParameter("#friend_username", friendusername));
if (Convert.ToInt32(cmd.ExecuteScalar()) > 0)
flag = true;
}
}
return flag;
If I'm not mistaken you're trying to count rows given a specific #username and #friendusername that have reciprocal values based on your conditions.
Below query should help, though - I have not tested it in action!
SELECT COUNT(*) as reciprocal_values_no
FROM table_name tbl1
WHERE
username = #username
AND friend_username = #friendusername
AND status = 0
AND EXISTS (
SELECT 1 FROM table_name tbl2 WHERE tbl1.username = tbl2.friend_username AND tbl1.friend_username = tbl2.username )
try this
select username,friend_username,status, count(*)
from table_name
group by username,friend_username,status
having count(*) > 1

Oledb update sql statement example

con.Open();
cmd = new OleDbCommand("UPDATE staff SET StaffNr = #a, FirstName = #b,
Surname = #c, Email = #d, Balance = #e WHERE Email = #d", con);
cmd.Parameters.AddWithValue("#b", textBoxStaffName.Text);
cmd.Parameters.AddWithValue("#c", textBoxStaffSur.Text);
cmd.Parameters.AddWithValue("#d", textBoxStaffE.Text);
cmd.Parameters.AddWithValue("#e", double.Parse(textBoxSatffBal.Text));
cmd.Parameters.AddWithValue("#a", int.Parse(textBoxStaffNr.Text));
cmd.ExecuteNonQuery();
con.Close();
not updating the database for some reason but the code runs
OldDb uses ? for parameters, not #x. You have to add parameters in order. The name is ignored in AddWithValue (this method comes from a general purpose superclass)
con.Open();
cmd = new OleDbCommand("Update staff set StaffNr = ?, FirstName = ?,
Surname = ?, Balance = ? Where Email = ?", con);
cmd.Parameters.AddWithValue("#a", int.Parse(textBoxStaffNr.Text));
cmd.Parameters.AddWithValue("#b", textBoxStaffName.Text);
cmd.Parameters.AddWithValue("#c", textBoxStaffSur.Text);
cmd.Parameters.AddWithValue("#e", double.Parse(textBoxSatffBal.Text));
cmd.Parameters.AddWithValue("#d", textBoxStaffE.Text);
cmd.ExecuteNonQuery();
con.Close();

SQL Server 2005 CE 3.5 Re seed IDENTITY

I have inserted some rows into a data table with
Set Identity_insert tblEvent on
I then attempt to 'reseed' the Identity field
int MaxId = this.MaxID()+1;
string upgrade = "ALTER TABLE " + Table + " ALTER COLUMN ID IDENTITY("+ MaxId.ToString() +",1)";
System.Data.SqlServerCe.SqlCeCommand cmd = new System.Data.SqlServerCe.SqlCeCommand(upgrade, connection);
cmd.CommandType = System.Data.CommandType.Text;
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
'MaxId' is determined by
int MaxId = 0;
string upgrade = "select Max(ID) from " + Table;
System.Data.SqlServerCe.SqlCeCommand cmd = new System.Data.SqlServerCe.SqlCeCommand(upgrade, connection);
cmd.CommandType = System.Data.CommandType.Text;
connection.Open();
MaxId = (int)cmd.ExecuteScalar();
connection.Close();
return MaxId;
However, if I query Max(ID) again after seeding it has'nt changed
Any idea's aprreciated
Try this:
string upgrade = " DBCC CHECKIDENT('[" + Table + "]', RESEED, " + (MaxId + 1)+ " )"
weird, could it be a permissions issue. you should have seen an exception though, unless the exception is gobbled up by a catch all.