How do I insert a row between here?
Data is not intended to be stored SQL tables in any particular order, so it's not appropriate to insert a row at a particular position. You use an SQL SELECT query to extract the data you want and ORDER BY to specify how it is sorted. If you really want to have this row in a particular position, add an ID column as the primary key and number the ID column values in the sequence that you want. Whatever you are using to view your rows will order them by the ID column by default. However, you're going to experience this same problem every time you want to add a new row as SQL tables are not intended to be used in this way.
Related
I have this fairly large DB. It contains lots of column. One of the will have a value that I need to select, but the DB has several of that value. How can I insert into a column in the row thats the newest in the DB, with a matching column.
Without knowing the ins and outs of your database, I think you would likely want to select the largest id you have in the auto incrementing row. For instance:
SELECT MAX(UNIQUE_ID) FROM TABLE WHERE MATCHING_COLUMN = MATCHING_VALUE
From there you can take your unique ID and insert into that row.
I have a normal table structure. Like the one below
TableName_OID|GlobalKeyColumn(or foreign key)|Column2| ... |ColumnN|CreateBy|CreateDate|ModifyBy|ModifyDate|IsActive|IsUpdated|UpdateOfTableName_OID
I have written a before update trigger which will take the last row (with inserted.TableName_OID) and insert into the table with its [IsUpdated] = 1 and [UpdateOfTableName_OID] = TableName_OID (or I can insert a row that will only have changed columns' values but I dont know how).
For showing the whole history of table wrt a [GlobalKeyColumn] I have something in my mind. If I go with the first option (inserting a whole row), I will have to run a cursor on the resultset (for a certain GlobalKeyColumn) comparing each row with it's previous one and show only the changed columns.
What I need help with both the options is how to compare each column of a row to its previous one's. I mean, I can do it manually, but is there any generic thing in SQL Server that could compare all the columns in a row to it's previous one?
i have 10 rows of data in my table. now i want to insert a row of data just after the 9th data and before row 10..if am not mistaken ,in mysql db this thing is possible before there's a before and after clause, how bout in oracle ?
here's my starting insert statement, let's say i want to insert it after the 9th data ,how?
INSERT INTO MYTABLE (TITLE,DESCRIPTION,STATUS) VALUES ('blahblah','descriptionblah',1);
There is no inherrent order to rows in a table, and there is no guarantee a particular ordering will be preserved between selects. Consider adding a column containing the required ordering and when querying the table, order the result set by that column.
To insert a row which should appear at a particular position in the result set, ensure that the value you place in the ordering column is appropriate.
How can i fetch column names from a table on index basis, like I want to make a tables whose column name should be the name of last column fields of a result set of a query, but those result sets last columns value may be different at different execution time, so i want to know how can i fetch those index value of that last column to make a temp table with column name of those last columns value of a result set.
Is there any way/function in sql server to dynamically form that?
sp_helpindex:
Reports information about the indexes
on a table or view.
You can also use ROW_NUMBER as explained here
What would be the correct universal SQL construct to get the last row inserted (or it's primary key). The ID might be autogenerated by a sequence but I do not want to deal with the sequence at all! I need to get the ID by querying the table. Alternatively, INSERT might be somehow extended to return the ID. Assume I am always inserting a single row. The solution should work with most RDBMS!
the best way is to depend on the sequence like:
select Max(ID) from tableName
but If you don't want to deal with it, you can add new timestamp column to your table and then select max from that column.
like this way
select Max(TimestampField) from tableName