Get most recent IDENTITY entry from SQL Server 2000 - sql

Yes, I still have a SQL Server 2000 box (I know, I know . . .). Can someone please remind me the most reliable/safe way to retrieve the latest IDENTITY entry from a newly inserted records from a particular table? I know how to do it in SQL Server 2008, etc, but I cant remember if its different from SQL Server 2000

SCOPE_IDENTITY. http://msdn.microsoft.com/en-us/library/aa259185(v=sql.80).aspx Don't EVER use ##IDENTITY. It should never be used for any reason EVER. Using it will cause you to break out in a rash, your wife to divorce you, and Dr. Who to be cancelled.
See: http://www.sqlfiddle.com/#!3/2e2d3/4 Behold the Heart of Darkness! Behold the evil that is ##IDENTITY! Use ##IDENTITY and lose your own!

Related

Migrating legacy data from SQL Server 2000 to 2019 , log block error - is there a painless way of moving over tables with autoinc identity columns? [migrated]

This question was migrated from Stack Overflow because it can be answered on Database Administrators Stack Exchange.
Migrated 5 days ago.
I've been tasked with migrating data from an instance of SQL Server 2000 to 2019. There are a total of four databases to bring over, three of which I was able to backup/restore into 2008 and then into 2019 without any issues. Please note: I am not a DBA in any sense, though I'm the closest thing to one on hand.
The fourth and final database presented the following error that prevented moving from 2008 to 2019:
System.Data.SqlClient.SqlError: An error occurred while processing the log for database 'DbNameHere'. The log block version 2 is unsupported. This server supports log version 3 to 6. (Microsoft.SqlServer.SmoExtended)
Is there a simple fix for this problem that I'm missing in the various SSMS menus?
Alternatively, is there a way to copy raw data from one server to another via, for instance, a flat file, and preserve the identity columns as identity columns? That is, I don't want to just strip that column and bulk insert, as they are often used as foreign keys in other tables, and with twenty-some-odd years of data, something is bound to break in doing this.
An example of an ideal final result in this solution would be something like: legacy table X has 1000 rows, the last of which has an identity column value of 1000. Once the move is complete, new table X has 1000 rows, the last of which has an identity column value of 1000, and upon insert the next row automatically increments to 1001.
Apart from unsuccessfully messing around with flat files, I've also tried the "Copy Database" option in SSMS, which also failed.
I would attempt to get SQL Server to rebuild the transaction log. Based on the error message, that might sort out the situation.
You first use sp_detach_db to detach the database. It is now very likely that the ldf file isn't needed when you do a subsequent attach, and perhaps rebuilding the log this way will sort the situation.
Then you attach the database, without the ldf file. Use CREATE DATABASE with either of the FOR ATTACH or FOR ATTACH_REBUILD_LOG options.
I would do this on the 2008 instance, since from what I understand you got the database in there successfully. But feel free to play around regarding on which version (2000 or 2008) you do the detach and also on which version (2000, 2008, 2019) you do the attach.

INSERT INTO SELECT Troubleshooting

When using INSERT INTO SELECT, the SELECT portion if run on its own returns in about 25 seconds. THE INSERT INTO I've let go for over 8 mins and then cancelled. What can I do to start troubleshooting this? I'm not sure if there is locking going on? This is a table that has constant single selects and inserts going on. There are also 3 index on this table that I know need to be updated when new rows come in. The scenario above is also for only 68,000 rows, and I do have other inserts that I will need to do that will contain more. Last, I'm using SQL SERVER 2008 R2.
Thanks in advance.
Is there space for the new table? I had a project way back in SQL Server 2000. The auto-growth was killing the timing. When I pre-allocated the space before loading, the process flew.
Make sure your growth options are not 1 mb or 10 %. That will kill you also.
Also, look into instant database file initialization. If this is turned off, the SQL Server engine has to zero out the pages before giving you new space. Otherwise, it skips this step.
Good link from MSDN on this topic,
http://msdn.microsoft.com/en-us/library/gg634626.aspx
Trace Flag 1806 which disables Instant Database File Initialization. Make sure this is not set on your server.
Please post back if this does not fix your issue.
Sincerely
John
The Crafty DBA
www.craftydba.com

How do i get the names of all the tables inside a database?

EDIT2: Found a fix! I used the number of the desired schema instead of the name. Should've thought of that before, really! And i think the error messages could've been a bit better aswell. Thanks for all your time!
How can i get the names of all tables inside a database through sql inside asp classic?
The server is running windows 2008, iis7.5 and microsoft jet. I've tried all the querys i could find on the internet (and here) but none have worked.
If i add a ; to the query to run a set of querys at the same time it gives me an error because the statement isn't over at the semicolon.
The master.mdf database cannot be accessed because it's of unknown format.
The sysobjects variable apparently doesn't exist.
I am using mssql 2000 format. (.mdf)
The connection is made through classic asp with the Microsoft.Jet.OLEDB.4.0 provider and ADODB connection/recordset.
How do I get list of all tables in a database using TSQL?
Query to get the names of all tables in SQL Server 2008 Database
EDIT:
I've found two folders containing databases. One is in C:\program files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\mssql\binn\templates and contains master.mdf, mastlog.ldf, model.mdf, modellog.ldf, msdbdata.mdf and msdblog.ldf. The other one is also in the \binn\template data directory and contains master.mdf, mastlog.ldf, model.mdf, modellog.ldf, MSDBData.mdf, MSDBLog.ldf, mssqlsystemresource.ldf, mssqlsystemresource.mdf, tempdb.mdf and templog.ldf. Maybe these is of some interest?
How can i tell if i have permission? Does it give a permission denied error?
Please help! No, don't. Read the 2nd edit at the top.
USE YOUR_DATABASE
GO
SELECT *
FROM sys.Tables
GO
Have you tried the example from:
http://www.kamath.com/codelibrary/cl002_listtables.asp
I almost always use the INFORMATION_SCHEMA views:
SELECT * FROM INFORMATION_SCHEMA.TABLES
If this isn't working for you, the SQL user your site is running under may not have access to the system objects. This is actually a good thing, as giving your site access to the underlying database schema can leave you vulnerable to SQL injection.
So if you do go this route, proceed with caution.
The mdf by itself is useless: you need a database engine (a.k.a. a SQL Server instance) to "run" it. As I understand the question, this is your problem.
Then you can use sysobjects in your database: unless you have added your tables to the master database
There is no practical way to use an mdf directly: if nothing else download MSDE

EF4 & SQL Server 2000

I've developed my website using EF4 and SQL Server 2005, but when moving to the staging site it turns out that they use SQL Server 2000.
Now I'm getting this error, which I believe is related to SQL Server 2000:
Incorrect syntax near '('. 'row_number' is not a recognized function name.
Is there a way of fixing this?
Thanks
EF v4 does not support SQL Server 2000. More details here:
https://connect.microsoft.com/VisualStudio/feedback/details/499186/entity-framework-v2-doesn-t-support-sql-2000
I came across the exact same problem and as per the comments/responses on StackOverflow I had almost given up and flicked it to client to upgrade their database to > 2000 version. Unfortunately that wasn't possible for them due to many other issues. I had to go around googling for a solution and from somewhere I found a work-around.
This is what I did.
Right clicked the ModelName.EDMX file -> Open With
Selected XML (Text) Editor
Found ProviderManifestToken="2005" and replaced it with
ProviderManifestToken="2000"
Published the changes and voila!
Now the reason I'm saying it's a work-around (not a solution) is because
If you update your model from database again and your development
machine database is >= 2000 (which is likely to be a case as SQL
2000 connection is not supported at all by MS according to this
article) this value in the XML will change automatically
Also, the queries generated by EF after this change have worked in
my situation but I cannot guarantee that all the queries ever
generated by your application would work without any problems
Row_Number() returns the sequential number for a each row returned. Are you calling Row_number(), or is the entity framework?
Either way, you may be able to write a user-defined function that re-implements ROW_NUMBER. Stackoverflow post on this here:
ROW_NUMBER Alternative for SQL Server 2000
Not sure this is going to do any good in the long run; you're likely to find that you get around this problem only to encounter the next reason that EF doesn't work on SQL 2000. But, it might be worth the few minutes to try.

How to find recent sql update operations acting upon a certain table (SQL Server 2005)

say I want to find the latest added rows (UPDATE by any user, not necessarily the one which is executing UPDATE) in XX table.
You would need to use a Transaction Log reader tool. There are several free ones available as well as commercial offerings.
ApexSQL Log
You could also try this undocumented command:
DBCC LOG(<database name>[,{0|1|2|3|4}]).
If you're using SQL Server 2000, RedGate have a free tool called SQL Log Rescue.
EDIT: Documentation for DBC LOG:
(1) (2)
Please refer to SQL Docs & look for OUTPUT clause (that you can use with UPDATE/INSERT to get the affected records).
http://msdn.microsoft.com/en-us/library/ms177564.aspx
SQL Server Profiler will allow you to track hits to the database in real time. You can set filters on a number of properties to ge the output you need.