I have a few questions below, to make it easier to identify the questions, I will bold them and give a number to each question (Q1, Q2, Q3...).
Let say I have a table like this :
Table Name : MyCustomer
Table Fields : CustID (varchar 10), CustName (varchar 10)
Current records on MyCustomer table :
CustID | CustName
-----------------
C12345 | Bruce
C12346 | Tom
On Delphi, I need to do a select, then an update.
For example Query1.SQL.Text is :
select CustID, CustName from MyCustomer where CustID = 'C12345'
update MyCustomer set CustName = 'Tom Riddle' where CustID = 'C12346'
Q1 - To achieve this, should I use Query1. Open or Query1.ExecSQL?
Since there is SELECT statement on this query (that return 1 record), then I assume I have to use :
Query1.Close;
Query1.Open;
Q2 - This way the CustName is successfully updated to Tom Riddle. Is this the correct approach?
But if I add a statement that move Query1's cursor, for example :
Query1.Close;
Query1.Open;
Query1.Last; // <----- cause error
I will get this error message NEXT time I run something to the SQL Server :
DB-Library error 10038: Attempt to initiate a new SQL Server operation with results pending.
Q3 - What is this error message mean? What are results pending mean? Why there are results pending?
Q4 - Is it ok if I use Query1.ExecSQL even if there is returned records from the SELECT statement? (I don't need to access the records, but I can't remove the SELECT statement from the query).
Q5 - From Embarcadero documentation :
Note: Do not use ExecSQL for commands that return data. These include any use of the ctTable command type, SELECT queries, and stored procedures that return a cursor. When the command returns data, use the Open method or set the Active property to true.
What is the reasoning behind this documentation? And what if there is INSERT / UPDATE / DELETE after the SELECT statement?
Related
I am using JTable to display the result of a query. Table does not show as XYZ for all columns but it shows XYZ as a header for the fields not existing in database(manipulated fields).
Don't know much of database internals.Please forgive if it's too basic.
rs1 = st1.executeQuery("SELECT product.`id` as `Product ID`,product.`serialnumber` as `Serial Number`, product.`dop` as `Date Of Purchase` FROM product where product.`dop` between '"+from+"' and '"+to+"'");
reportTable.setModel(buildTableModel(rs1));
same query on query browser Output:
Product ID Serial Number Date Of Purchase
1 123244mf43m 08/08/2013
My Output With JDBC is:
id serialnumber dop //table header
1 123244mf43m 08/08/2013
There is a configuration setting described here:
http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-configuration-properties.html
useOldAliasMetadataBehavior
which, if set to true (the default in 5.0.x) will only return aliases (if any) for ResultSetMetaData.getColumnName() or ResultSetMetaData.getTableName() rather than the original column/table name.
Not sure if this is applicable to you, but could be the cause.
I have a table named platform with a column named entityid. The data in entityid is supposed to adhere to the format n.n.n (where n = a number of 1 or more numerals, the first number is a site ID).
If i run this query:
SELECT count(*) FROM platform
I get: 16063
So I've got 16063 rows in my table. When I try to filter for only site 18 I run this query:
SELECT count(*) FROM platform
where entityid like '18.%.%'
I get: 4454
So far, so good. But if I try to find platforms not at site 18:
SELECT count(*) FROM platform
where entityid not like '18.%.%'
I get: 11608
Here's the problem: 4454 + 11608 = 16062
I'm missing a record. I think I'm getting all the platforms that are at site 18, and then all the platforms that are not at site 18 - how am I missing one record?
The problem is probably a null value. Try this and see if it returns a record:
select *
from platform
where entityid is null;
NULL values fail almost all comparisons (except for is null).
I have one table named task_assignment.It has following 6 fields named as:
testId,quesId,evaluatorId,studId and marks
Actually this table is used to store marks for each test including each evaluators marks for each students by question id wise.
I have testId=1, quesId=Q1 and studId=S1 as a input. So, i want to get the following information in the select query.ie,Both evaluators(E1,E2) marks for the given input.
The sql query don't written more than one row for this...I want query output is :20,15 in a single row.
Please guide me to get out of this issue...
I think you won't be able to get your desired output 20, 15, since there is only one record which satisfies your criteria testId = 1, quesId = Q1, studId = S1.
But to answer your question, here's my query:
SELECT GROUP_CONCAT(marks)
FROM task_assignment
WHERE testId = 1
AND quesId = 'Q1'
AND studId = 'S1';
I've tried it in SQL Fiddle.
EDIT 1
If you want to parse the output of the query in your C# code to store them in separate variables, you can use the Split function:
string marks = "20, 15"; //Suppose that this value came from database
int mark1 = Convert.ToInt32(marks.Split(',')[0]);
int mark2 = Convert.ToInt32(marks.Split(',')[1]);
The code is still error-prone depending on the value of the marks variable, just make sure you have validated the value.
This might be unrelated to the question, but still to help you on your task, that's my answer.
I am writing a MS Access Query with Parameters and wondered if it were possible to include one of the parameters as the returned selected records:
PARAMETERS [#SubmissionID] IEEEDouble, [#StartDate] DATETIME, [#EndDate] DATETIME;
INSERT INTO tblHUD_client_profile
(SubmissionID, ClientID)
SELECT [#SubmissionID] as SubmissionID, DISTINCT(ClientID)
FROM tblClientEducation
WHERE (BeginDate BETWEEN [#StartDate] AND [#EndDate]
OR EndDate BETWEEN [#StartDate] AND [#EndDate])
AND NOT EXISTS(
Select ClientID
from tblHUD_client_profile
WHERE SubmissionID = [#SubmissionID]
AND ClientID = tblClientEducation.ClientID
);
The "Select [#SubmissionID] as SubmissionID" always gives me a syntax error.
I apologize if there's a question with this solution already out there. I looked around for this but with so many basic questions about MS Access Queries and Parameters, I couldn't find what I was looking for.
Leave out # for MS Access:
PARAMETERS SubmissionID Integer;
SELECT [SubmissionID] as SubmissionID, DISTINCT(ClientID)
FROM tblClientEducation
After reviewing the original code, it seems to work as expected.
First of all I'm rather new to SQL and so even though I believe a similar question was asked in this thread ( SQL Query - Copy Values in Same Table ) I literally can't understand it well enough to utilize the information. For that I apologize.
Now, I have a table that looks something like this:
company id | parameter name | parameter title
P | Parameter One | First Parameter
P | Parameter Two | Second Parameter
P | Parameter Three| Third Parameter
W | Parameter One | NULL
W | Parameter Two | NULL
Except that my table obviously has quite a lot of rows. I already went through filling in all the parameter titles where the company id was 'P' and would like to avoid manually doing the same for those with company id 'W'. My question is what SQL statement (this is in Microsoft SQL Server 2008) can I use to copy the values in the column "parameter title" where the company id is 'P' to the values in the same column where the company id is 'W' and both parameter names match up (W has less parameters than P)?
Using the previously linked thread I was able to come up with the following, but it spits out an error and I know it's not done correctly:
UPDATE COMP_PARAMETER_COPY
SET PARAM_TITLE=(SELECT PARAM_TITLE FROM COMP_PARAMETER_COPY P
WHERE P.COMP_ID = 'P' AND P.PARAM_TITLE=PARAM_TITLE)
WHERE COMP_ID='W'
(I'm playing around with a copy of the table instead of the actual table)
The error I get is "Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated."
Thank you for your help and advice,
-Asaf
You need to ensure that your subquery is only returning one result. Right now that error message is telling you that you're getting more than one record returned.
UPDATE W
SET PARAM_TITLE = (
SELECT PARAM_TITLE FROM COMP_PARAMETER_COPY P
WHERE P.COMP_ID = 'P' AND P.PARAM_NAME = W.PARAM_NAME
)
FROM COMP_PARAMETER_COPY W
WHERE W.COMP_ID = 'W'
Try giving the above SQL a whirl. This could still give you more than one result, but without knowing what your table looks like and what the data constraints are it's hard to give you something guaranteed to work.
Try adding the DISTINCT keyword to your query:
UPDATE COMP_PARAMETER_COPY
SET PARAM_TITLE=(SELECT DISTINCT PARAM_TITLE FROM COMP_PARAMETER_COPY P
WHERE P.COMP_ID = 'P' AND P.PARAM_TITLE=PARAM_TITLE)
WHERE COMP_ID='W'