Archive

Posts Tagged ‘MSSQL’

Set Identity on existing column

September 23, 2009 ppshein Leave a comment

Database of one of our project is stored by Microsoft Access. As you know, MS.Access can’t store much of data amount in it. Lack of such appearance, our website is spontaneously down whenever users surfing through it. That’s why we need to upgrade our DBMS to MsSQL. So, I’m in rush of doing so. First of all, I need to export all data as XML file. Then, import these data to Ms.SQL. But I wanna keep certain ID column of each tables. That’s why I import all data as OFF identity Mode. After importing, I need to turn ON. At that time, I’m in trouble. NO idea to do how to turn ON identity and thinking of various ways. Eventually, I gotcha.

Here we go..

<cftransaction>
<!– Creating Temp Table –>
<cfquery name=Items datasource=”#application.ds#” password=”#application.pw#” username=”#application.un#”>
CREATE TABLE Categories2
(
C_ID int primary key identity(1,1),
C_Name nvarchar(500),
C_SortOrder int,
C_Text nvarchar(4000),
C_Parent int
)
</cfquery>

<!– Set OFF Identity  –>
<cfquery name=Items datasource=”#application.ds#” password=”#application.pw#” username=”#application.un#”>
SET IDENTITY_INSERT Categories2 ON;
</cfquery>

<!– Migrate Data –>
<cfquery name=Items datasource=”#application.ds#” password=”#application.pw#” username=”#application.un#”>
INSERT INTO Categories2(C_ID, C_NAME, C_SortOrder, C_Text, C_Parent)  SELECT C_ID, C_NAME, C_SortOrder, C_Text, C_Parent FROM Categories
</cfquery>

<!– Set On Identity –>
<cfquery name=Items datasource=”#application.ds#” password=”#application.pw#” username=”#application.un#”>
SET IDENTITY_INSERT Categories2 OFF;
</cfquery>

<!– Drop old table –>
<cfquery name=Items datasource=”#application.ds#” password=”#application.pw#” username=”#application.un#”>
DROP TABLE Categories
</cfquery>

<!– Rename Table –>
<cfquery name=Items datasource=”#application.ds#” password=”#application.pw#” username=”#application.un#”>
SP_RENAME Categories2, Categories;
</cfquery>
</cftransaction>

Categories: MSSQL, Projects, coldfusion Tags: ,

Limit tag in MsSQL 2005

April 23, 2009 ppshein Leave a comment

Hope everybody knows about limit tag MsSql 2005. I feel that’s cool and can reduce the load of SQL query just like mySQL. It’s kinda simple to use this tag.

SELECT * FROM myTable limit 1,10

If we write so, the sql will return starting from the first record, and plus 10 record in the recordset of myTable.

SELECT * FROM myTable limit 20,40

If we write so, the sql will return starting from the 20th and plus 10 record of myTable. How.? It’s easy, isn’t it?

Categories: MSSQL Tags: , ,

Single Attach MDF MSSQL

October 31, 2008 ppshein Leave a comment

Some many projects drive me so far to update my blog in these days. So, I want to update my blog when I get free time. In this post, I’ll describe how to reduce our storage size by the way of deleting Log File (.ldf) of MsSQL. Sometimes, Log file is bigger than Data File (.mdf) because it save all transaction of whatever we do to our database. That’s why I want to delete log file (without having risks) and create new log file without having log records. Yap, here we go. First of all, we need to detach our database. Then, delete .ldf file. (if you’re not sure, backup this log file). Then, open Query Analyzer and type following coding.

EXEC sp_attach_single_file_db @dbname = ‘[db_name]‘,
@physname = ‘C:\MSSQL\Data\[mdf_file_name]‘

After that, run it. SQL will create new log file for you. How? Easy. Isn’t it?

Categories: MSSQL Tags: ,

passed to log scan mssql 2000

August 21, 2008 ppshein Leave a comment

I was dilemma last weekend. Now that our server has faced such error message “passed to log scan” while attaching database with MS.SQL 2000 enterprise manager. For this case, I was stack on this error and trying to get data from *.mdf file on anyway like seeking in Google. Fortunately, I’ve found www.ureader.com pointing out these kinda error message, how to solve it.

I did the instruction of this blog. But, it was not fine and showed my database is marked as Emergency mode. At that time, I have had an idea to retrieve the included datas from *.mdf with by the way of using SQL Query Analyzer. Thanks God. Cause I can retrieve all included data by Query Analyzer but one left is how to migrate the data from the database marked as Suspend mode to another new database. I was supposed to set all data into text output with Query Analyzer or something else. But, anything would be ok. Finally, I’ve create black database and using follow query string to migrate new database as to recover my database.

SELECT * INTO NewTable FROM existingdb.dbo.existingtable

Best Credit to :http://www.ureader.com/msg/1145136.aspx http://snippets.dzone.com/posts/show/372

Categories: MSSQL Tags: , ,