I have a database with a question table each question has a level attribute, topic, and the answers. I want to pick up randomly question in an ASP.net project but i don't want the same question to be repeated in the Details View.
This is the select statement:
SELECT TOP 3 [Question Number] AS Question_Number
,[Question Title] AS Question_Title
,[Answer 1] AS Answer_1
,[Answer 2] AS Answer_2
,[Answer 3] AS Answer_3
,QuizID
,Level
FROM Question
WHERE ( Level = 1 )
ORDER BY NEWID()
I don't think so you will get duplicate row until unless you have duplicate record in table. if you have then use DISTINCT to get unique record from table.
The basic syntax of DISTINCT keyword to eliminate duplicate records is as follows:
SELECT DISTINCT column1, column2,...columnN FROM table_name WHERE [condition]
http://www.w3schools.com/sql/sql_distinct.asp
Related
In my database, i have a trigger which insert the change log entries when a row in Table tblA is updated.
Now, in my code i have to update it through a plain Sql query like
int count = DBContext.ExecuteStoreCommand("<sql query to update records>");
This count variable contains the number of rows affected(no of rows updated + no of rows inserted) due to query.
So my question is, How do i can get only the number of updated rows?
Currently i'm using Entity framework 4. I have looked for solution through connected or disconnected model but couldn't help myself.
int count = DBContext.ExecuteStoreCommand("");
I think you hv to change this to return Select result set
then do this,
<sql query to update>
Select ##RowCount rowcountAffected
Or
suppose your update is
update table1 set col1='foo' where id=2
select count(*) rowcountAffected from table1 where id=2
The most efficient way to return row affected can be
i) Assuming you only update (don't refresh any record after that)
Put Set Nocount ON
Declare #Output parameter inside proc
I'm not sure I'm using right terminology here.
Basically I want to update entire "id" column using count(*) [485] as a delimiter, in an ascending order, so the resulting row value will correspond with rownumber (not the rowid).
If I understand you correctly, this should work for you:
UPDATE tbl_name SET id=rowid
EDIT
If that's is the case -> then it's a lit bit more tricky, since SQlite doesn't support variables declaration.
So what I suggest is,
To create temporary table from select of your original table which makes it's rowids to be as row numbers 1,2,3 etc...
Set it's rowNum (the needed row number column) as each rowid
Then replace the original table with it.
Like this: (assume original table called orig_name)
CREATE TABLE tmp_tbl AS SELECT rowNum FROM orig_name;
UPDATE tmp_tbl SET rowNum=rowid;
DROP TABLE orig_name;
CREATE TABLE orig_name AS SELECT rowNum FROM tmp_tbl;
DROP TABLE tmp_tbl;
Try this: http://www.sqlite.org/lang_createtable.html#rowid
You can use some inner database variables such as rowid, oid etc to get what you need.
[edit]
Think I just understood what you meant, you want for each insert action, add a value that is the total count of rows currently in the table?
If so, try something like this:
UPDATE tbl_name
SET id = (select count(*) from tbl_name)
WHERE yada_yada_yada
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a select query which returns 50 results. The select query returns id .
The next part i need to run a select query which value for all the 50 ids.
Please can some one tell me what is the best way to get it done.
is using a for loop a good idea
e.g.
begin
declare #count= select id from table1
for 1 to #count ..loop
select value from table2 where id =1
end loop
Can i use cursors what is the best method.
Please elaborate on this statement "The next part i need to run a select query which value for all the 50 ids."
However, if you want to fetch those 50 records in a pl/sql block and manipulate them in some way, you would require a cursor. Records from cursor can be fetched both with and without FOR loop.
Cursor example:
declare
cursor c1 is select col1 from table1;
begin
for a in c1
loop
dbms_output.put_line(a.col1);
end loop;
end;
The above piece of code would display the values from col1 when server output is on.
In a simple case such as you suggest I think it's a bad idea to build up a collection of values and then iterate through them to fetch data from a second table. It's a better idea to use a JOIN to, well, join together the data from the two tables based on whatever criteria you've got. In this case it appears you're select an ID value from TABLE1 which is also on TABLE2. Thus, you might consider something like the following:
FOR myRow IN (SELECT t1.ID, t2.VALUE
FROM TABLE1 t1
INNER JOIN TABLE2 t2
ON (t2.ID = t1.ID))
LOOP
-- Do something useful with the data
DBMS_OUTPUT.PUT_LINE('t1.ID=' || myRow.ID || ' t2.VALUE=' || t2.VALUE);
END LOOP;
Share and enjoy.
Given below can be onw good waye
declare
cursor c1 is select value from table2 where id in (select id from table1)
begin
for i in c1
loop
...
...
end loop;
end;
This seems like a simple request. But my query in not working and I'm finding conflicting answers on the internet. Is it possible to have UPDATE and INSERT using a Stored Procedure joining 2 tables in MySql?
I have an Asp.net Webforms website. It has 2 tables Individual and Address. Individual table contains data on an individual, i.e. Phone Number, Fax, Email ect.
The address table has all the address information for an individual. They each table has a column of Individual ID which auto increments. (Note: the individualID in Address table is not a primary key, but individualID in the individual table is a primary key.
Anyway, I have a FormView in Asp.net that with a SELECT statement that joins those 2 tables and display the data fine. But updating new information to both tables keeps failing.
My most recent error is : Duplicate entry '0' for key 'PRIMARY'
Is there a way to write an UPDATE statement that joins 2 Tables?? This has to exist right?
It is possible to update multiple tables with a single query -
UPDATE table1
INNER JOIN table2
ON table1.id = table2.table1_id
SET table1.col1 = 'some value', table2.col1 = 'Another value'
WHERE <some where clause>;
Maybe i should do this in C# but i have more then one row with linkId X. I would like to remove it but i am unsure how. In code i could just use a foreach from 0 to n and remove any found rows with a greater (or !=) id but thats in code. Is there a less difficult way of doing it using sqlite?
Assuming the table's name is tableName and there is a primary key field named id, the following sql would do it. I think the following SQL query is general enough and should be able to be executed under any database engine.
delete from tableName
where id not in (
select min(id) from tableName
group by linkId
)