SQL Server 2008 R2 Express multi-part could not be bound - sql

I know similar questions has been before but I just couldn't quite understand them, still very new to SQL and wanting to get the solution as well as understanding why it wouldn't work originally.
sql = "UPDATE e " +
"SET " +
"e.Operator = '" + emp.Operator + "', " +
"e.LoginName = '" + emp.Login + "', " +
"e.Active = " + (emp.Active == true ? 1 : 0) + "," +
"e.Position = '" + emp.Position + "', " +
"p.Admin = " + (emp.Permission.Admin == true ? 1 : 0) + "," +
"p.Manager = " + (emp.Permission.Manager == true ? 1 : 0) + "," +
"p.Overtime = " + (emp.Permission.Overtime == true ? 1 : 0) + "," +
"p.TimeInLieu = " + emp.Permission.TimeInLieu + " " +
"FROM Employee e INNER JOIN Permissions p " +
"ON e.PermissionID = p.PermissionID AND e.Operator = '" + employee + "'";
when trying to execute the command I get this error.
The multi-part identifier "p.Admin" could not be bound.
Any help would be greatly received.

Related

Sql Three Table Inner Join?

I'm trying to join 3 tables in a view; here is my situation:
I have a table that contains Sale it Contain Sale Details For per Item
Another Sale Master A All Sale Details of All Item..
And Another Inventory Details
String query = "SELECT SALE.ITEM_CODE, SALE.ITEM_NAME, SALE.UNIT, "
+ "SALE.QNTY, SALE.AMOUNT, SALE_MASTER.LONGDATE, SALE_MASTER.BILL_NO, "
+ "SALE_MASTER.LEDGER_CODE, SALE_MASTER.LEDGER_NAME FROM SALE "
+ "INNER JOIN SALE_MASTER"
+ " ON SALE.BILL_NO = SALE_MASTER.BILL_NO SALE"
+ "INNER JOIN INVENTORY ON SALE.ITEM_CODE = INVENTORY.ITEM_CODE"
+ "WHERE "+CATORINORG+" LIKE '%"+LIKE+"%' "
+ "AND (SALE_MASTER.LONGDATE >= " + From + " AND SALE_MASTER.LONGDATE <= " + To + ")";
is it right way.. thanks adv
SELECT SALE.ITEM_CODE, SALE.ITEM_NAME, SALE.UNIT, "
+ "SALE.QNTY, SALE.AMOUNT, SALE_MASTER.LONGDATE, SALE_MASTER.BILL_NO, "
+ "SALE_MASTER.LEDGER_CODE, SALE_MASTER.LEDGER_NAME FROM SALE "
+ "INNER JOIN SALE_MASTER"
+ " ON SALE.BILL_NO = SALE_MASTER.BILL_NO"
+ " INNER JOIN INVENTORY ON INVENTORY.ITEM_CODE = SALE.ITEM_CODE"
+ " WHERE "+CATORINORG+" LIKE '%"+LIKE+"%' "
+ "AND (SALE_MASTER.LONGDATE >= " + From + " AND SALE_MASTER.LONGDATE <= " + To + ")
its worked for me

SQL Join Query taking long to complete

I am working on a project that require me to join four tables. I have written this code but it's taking forever to finish. Please help. Ohhh I have about 121 000 entries in the Db
PortfolioCollectionDataContext context = null;
context = DataContext;
var Logins = from bkg in context.EnquiryBookings
where bkg.Paid == true
from log in context.Logins
where log.LoginID == bkg.LoginID
from enq in context.Enquiries
where enq.EnquiryID == bkg.EnquiryID
from estb in context.Establishments
where enq.EstablishmentID == estb.EstablishmentID
select new
{
log.LoginID,
log.FirstName,
log.LastName,
log.CountryOfResidence,
log.EmailAddress,
log.TelephoneNumber,
bkg.TotalPrice,
estb.CompanyName
};
string str = "";
foreach (var user in Logins)
{
str += ("[Name: " + user.LastName + " " + user.FirstName + " - Country: " + user.CountryOfResidence + " - Phone: " + user.TelephoneNumber + " - Email: " + user.EmailAddress + " - Booked From: " + user.CompanyName + " - Spent: " + user.TotalPrice.ToString() + "]");
}
return str;
Use following query on LINQ
PortfolioCollectionDataContext context = null;
context = DataContext;
var Logins = from bkg in context.EnquiryBookings
join log in context.Logins
on log.LoginID equals bkg.LoginID
&& bkg.Paid == true
join enq in context.Enquiries
on enq.EnquiryID equals bkg.EnquiryID
join estb in context.Establishments
on enq.EstablishmentID == estb.EstablishmentID
select new
{
str = "[Name: " + log.LastName + " " + log.FirstName + " - Country: " + log.CountryOfResidence + " - Phone: "
+ log.TelephoneNumber + " - Email: " + log.EmailAddress + " - Booked From: "
+ estb.CompanyName + " - Spent: " + bkg.TotalPrice.ToString() + "]"
};
string output = string.Join(", ", Logins.ToList());
return output;
Check your query by taking cursor on Logins and paste that query here. Then check an estimated execution plan of your query and paste here.
If using SQL Server, to get execution plan of your query using Sql server management studio, click on an icon highlighted in an image below.

SQL unable to cast object of type 'system.string' to type 'system.iformatprovider' error in vb.net

I am trying to run this Query in my VB Application but receive an error saying:
unable to cast object of type 'system.string' to type 'system.iformatprovider'
SQL = "insert into billing_pdf_archive (reseller_sequence, invoice_number, pdf, worddoc, csv_cdr_file, csv_services_file, sub_total, vat_amount, grand_total, invoice_type, directdebit) values ('" + reseller.ToString + "','" + invoice_number.ToString + "', '" + Replace(reseller_company_name + "-" + invoice_number + ".pdf", " ", "_") + "', '" + Replace(reseller_company_name + "-" + invoice_number + ".doc", " ", "_") + "', '" + Replace(reseller_company_name + "-" + invoice_number.ToString + "_CDR.xlsx", " ", "_") + "', '" + Replace(reseller_company_name + "-" + invoice_number.ToString + "_Services.xlsx", " ", "_") + "', " + total.ToString("F2") + ", " + vat_amount.ToString("F2") + ", " + grand_total.ToString("F2") + ", 'Month End Reseller', '" + customer_direct_debit + "')"
conn3.ConnectionString = "server=" + global_variables.web_server_ip + "; user id=" + global_variables.web_server_username + "; password=" + global_variables.web_server_password + "; database=" + global_variables.web_server_database + "; "
conn3.Open()
myCommand3.Connection = conn3
myCommand3.CommandText = SQL
myCommand3.ExecuteNonQuery()
conn3.Close()
This is not a complete answer but I'll post it as an answer so that I can post formatted code. If you do as suggested in the comments and write clean, readable code then it will become obvious where the issue is and how to fix it. When you have one line that does lots of different things then working out what on that line is causing an issue is all but impossible. You should use an XML literal for your SQL code, parameters for your values and a connection string builder, e.g.
Dim sql = <sql>
INSERT INTO MyTable
(
Column1,
Column2
)
VALUES
(
#Column1,
#Column2
)
</sql>
command.CommandText = sql.Value
command.Parameters.AddWithValue("#Column1", value1)
command.Parameters.AddWithValue("#Column2", value2)
Dim builder As New SqlConnectionStringBuilder
builder.DataSource = server
builder.InitialCatalog = database
connection.ConnectionString = builder.ConnectionString
Now you'll be able to see exactly what part of your code is causing the issue and, if you still can't solve it yourself, will be able to point out where the issue is to us instead of expecting us to read that dog's breakfast.

SQL Inner Join query returns no results

I'm refactoring a Java program written by someone else a couple of years ago, can't get in contact with them to find out anything about the SQL / database, but this query is not working (not returning any results when having two queries separately does). I know it's annoying to ask without more info, but I haven't really got much choice at the moment.
"SELECT " + CLMHDR + ".POLBRC, "
+ CLMHDR + ".POLTYC, " + CLMHDR + ".POLNOC," + CLMHDR + ".CLTKYC, "
+ POLHDR + ".INCPTP FROM "+ CLMHDR +
"INNER JOIN " + POLHDR + " ON " + CLMHDR + ".CLTKYC = " + POLHDR+ ".CLTKYP"
+ " WHERE POLNOC = "+ polnocSearch
+ " AND POLBRC = '" + polbrcSearch + "'"
+ " AND POLTYC = '" + poltycSearch + "'"
+ " AND DATRPC <= " + claimDate
+ " GROUP BY POLBRC, POLTYC, POLNOC, CLTKYC"
The tables CMLHDR and POLHDR do contain the columns it is referencing, and CLTKYC and CLTKYP are keys in each table. Sorry about the horrible names, we're stuck with RPG as well.
Edit:
What does work is this:
"SELECT POLBRC, POLTYC, POLNOC, CLTKYC FROM "+ CLMHDR
+ " WHERE POLNOC = "+ polnocSearch
+ " AND POLBRC = '" + polbrcSeach + "'"
+ " AND POLTYC = '" + poltycSearch + "'"
+ " AND DATRPC <= " + claimDate
+ " GROUP BY POLBRC, POLTYC, POLNOC, CLTKYC"
followed by this:
"SELECT INCPTP, TRMTHP FROM "+ POLHDR + " WHERE POLNOP = "+ polnocSearch
+ " AND POLBRP = '"+ polbrcSearch+ "' AND POLTYP = '"+ poltycSearch + "'"
but I'd really prefer all the data to be returned at once.
There is a space missing between the FROM and the INNER JOIN clause:
FROM "+ CLMHDR +
"INNER JOIN
It is should be this:
FROM "+ CLMHDR +
" INNER JOIN
In addition to the inner join problem, you have an issue with the group by. It should have INCPTP. In any database except for MySQL, this will generate an error.
By the way, it would be easier to answer your question if it included two things:
The database engine you are using
The resulting query string with the values filled in

Assignment error is thrown in the query

I have a query like this....
selectLeaveDetails =
"SELECT UL.[PK_ID],UD.FIRST_NAME + ' ' + UD.LAST_NAME AS REQUESTBY," +
"UL.[DATE_FROM] AS FROMDATE,UL.[DATE_TO] AS TODATE," +
"UL.LEAVE_REQUEST_ON AS REQUESTON," +
"REPLACE(UL.LEAVE_REQUEST_NOTES, '\n', '<br />') AS REQUESTNOTES," +
"STATUS=CASE " +
" WHEN UL.[LEAVE_STATUS] = '1' THEN 'ACTIVE' " +
" WHEN UL.[LEAVE_STATUS] = '-1' THEN 'CANCELLED' " +
" WHEN UL.[LEAVE_STATUS] = '2' THEN 'REPLACED' END," +
"UL.LEAVE_RESPONSE_ON AS RESPONSEON," +
"ULL.FIRST_NAME + ' ' + ULL.LAST_NAME AS RESPONSEBY," +
"UL.[LEAVE_RESPONSE_NOTES] AS RESPONSENOTES,UL.FK_LEAVE_REQUESTER " +
"FROM (M_USER_LEAVES UL " +
"INNER JOIN M_LEADERLED MLL ON MLL.LED_ID = MUD.PK_ID WHERE MLL.LEADER_ID = '" + Session["UserID"].ToString() ****" +****
"LEFT JOIN M_USER_DETAILS UD ON UD.PK_ID = UL.FK_LEAVE_REQUESTER) " +
"LEFT JOIN M_USER_DETAILS ULL ON ULL.PK_ID = UL.FK_LEAVE_RESPONSE_BY " +
" WHERE UL.DATE_FROM BETWEEN '01/01/" + cmbYearList.SelectedItem.Text + "' AND '12/31/" + cmbYearList.SelectedItem.Text + "'" +
" AND UD.ACTIVE=1";
In the cmbYearList.SelectedItem.Text + "' AND '12/31/" + cmbYearList.SelectedItem.Text + "'" query...only assignment,increment,decrement error is thrown
Can anyone help me?
Your FROM clause is somehow pretty mangled up:
FROM (M_USER_LEAVES UL
INNER JOIN M_LEADERLED MLL ON MLL.LED_ID = MUD.PK_ID
WHERE MLL.LEADER_ID = 'XXXX"
LEFT JOIN M_USER_DETAILS UD ON UD.PK_ID = UL.FK_LEAVE_REQUESTER)
You have an INNER JOIN, then a WHERE clause, followed by a LEFT JOIN .... this seems pretty odd..... what exactly are you trying to do here?? Why do you need to put this into a subquery - can't you just INNER JOIN and LEFT JOIN those tables into a single statement and define the necessary WHERE constraints?
Also, your WHERE clause in here gets an opening single quote and a closing double quote - that won't work ......
WHERE MLL.LEADER_ID = 'XXXX"
*** ***
You need to get your SQL query working in SQL Server Management Studio first - then transfer it into your C# code.