Tag: mssql
Copy a Table into another
by z3n on May.12, 2010, under Tips & Hints
Problem:
How to copy a table into another, like a fast backup of a single table.
Solution:
MySQL:
create table <destination> select * from <source>
MSSQL:
select * into <destination> From <source>
Notes:
Although primary keys are preserved, the primary key column will not be automatically set on the destination table, so watch out before start inserting content on the destination.
ASP LCID
by z3n on Oct.02, 2009, under Coding, Tips & Hints
Problem:
Date format is wrong on asp.
Solution:
ASP’s LCID (location configuration) is wrong, depending on what is the regional configuration of the machine you’re running your asp into the date format might be different. Thanks to microsoft.
There’s a workaround for this by setting the Session.LCID at global.asa:
Sub Session_OnStart
Session.LCID = 1033
End Sub
Will set the region to English – United States (1033).
You can see a full list on the source I’ve found this information, here.
Full Story:
Datetime formats were wrong on the MSSQL database after the migration, after doing some research I’ve figured out that MSSQL has it’s own datetime format which also might vary depending on the specific windows regional configurations, to know what is the format the MSSQL is running you should query sp_configure ‘default langauge’, config_value column will return the code of the language which you can find by sp_helplanguage, it also has the default datetime format, which you can set on the specific instance by SET DATEFORMAT mdy.
variable Object MSSQL note
by z3n on Sep.25, 2009, under Notes
Problem:
return $res->Fields['field_name']->Value;
returns `variable Object`
Solution:
return intval($res->Fields['field_name']->Value);
Explanation:
There’s no explanation, this is one of those issues that happens when you deal with microsoft. Sometimes when doing a query it returns the actual value, other times it just return into a different variable type, which needs to be converted. Good thing if you caught it before writing a more complex code and then finally dealing with crypt errors :)
Bonus:
I’ve been getting some more issues even when using this conversion, things like “Object of class variant could not be converted to string php“, this happens on null values, in order to fix i been searching around and found this, which with some changes it will be better like:
-
for ($col=array(),$i=0,$j=$ms_res->Fields->Count();$i < $j;$i++) {
-
try {
-
$value=$ms_res->Fields[$i]->Value;
-
if (trim($value) != "") {
-
if (gettype($value) !== 'string') {
-
if (settype($value,"string")) {
-
$col[$ms_res->Fields[$i]->Name]=$value;
-
}
-
} else {
-
$col[$ms_res->Fields[$i]->Name]=$value;
-
}
-
} else {
-
$col[$ms_res->Fields[$i]->Name]='NULL';
-
}
-
} catch (exception $e) {
-
_r("Exception: ".$e);
-
}
-
}
..and yes you will figure out the missing functions/vars.
Reset Auto Increment
by z3n on Sep.15, 2009, under Tips & Hints
Problem:
How to reset a auto_increment / identity value on a table?
Solution:
MySQL: alter table tablename auto_increment=1234;
MSSQL: dbcc checkident(‘tablename‘,RESEED,1234);
Microsoft SQL+IIS Migration
by z3n on Sep.04, 2009, under Notes, Tips & Hints
Problem:
Migrate a SQL 2000 DB to a 2005 Server and IIS 6.0 to IIS 7.0.
Extra Issues:
- SQL2000 is full of procedures, views and identity tables (+200 each)
- Coder who built tables/procedures was lame, so there might be NULL values where it shouldn’t among with other things, like STRINGs being converted to DATE, INT, etc.
- Also includes lame asp coding.
- CRYPT MICROSOFT ERRORS (CRITICAL)
More Problems:
I don’t know why people still using microsoft, everything is against it. Not only the OS, but everything (ASP, SQL, VB, IE, Office, etc) related to microsoft is bad.
When you start to get those `error: XXXXXXXXX` like you know you’re screwed, why:
- Error codes aren’t clear and used by many different instances.. guess what, none of them will fit your issue;
- Microsoft KB isen’t accurate;
- Sometimes, Microsoft KB might present you the right answer, but it will be the last one on a list of 10 possible solutions, since they don’t have much control on the crypt errors themselfs, no wonder;
- Microsoft KB likes to do inaccurate automatic translations of the content (if your not english native), meaning that, if you eventually forgot that, you will be lead to another issue caused by the automatic translation.
- MSSQL don’t have `limit x` on delete/update queries, unless you do a huge workaround that will lead to more issues.
- IIS complies with MSSQL, also showing crypt errors, making the debugging impossible for non-computer beings (eg.: you).
- IIS crypt errors can be more funny when it simple shows `Bad Request` or `Application Error` and NOTHING ELSE.
- IIS likes to get stuck, if you try to restart/stop/start/recicle a service that takes too much time to happen, IIS manager simply shows a message `Service not responding` and abort the operation, sometimes it abort the operation and a few minutes later it happens, other times it just don’t happen and/or get stuck then you need to reboot;
- Copying database straight from the 2000 server to a local 2005 server gives the following error: `Error, please check your log`; Checking `your log` gives lots, lots of crypt non-sense errors in my native language, guess what? 0 results on google.
- The error above takes 30 minutes to happen, nothing is copied, but there’s a 300mb .mdb file.
- If you do a `SCRIPT DATABASE` you will not get the whole data or/and tables will not have identity columns or/and some data may be corrupted (even deatched);
- If you do a `IMPORT/EXPORT` procedure, you will fall on the VERY SAME problems above, but now you may also get timeout crypt message;
- You can only edit top 300 lines on MSSMS, sorted by MSSMS’ will;
- [db_name].[dbo].[tbl_name] will not appear on the automatic generated scripts, but if you forget to add it your script will miserabily fail with a non-crypt error message (first time) — that only happens if you use MSSMS;
- Can’t stand that green circle looping;
- MSSMS 2005 and 2008 are about the same, meaning you will have the same errors, so don’t even try to update;
- SQL2005 SP3 must be installed before anything, unless you want to fall on unsolved crypt errors, leading to nothingness;
- You can also amazingly fall into crypt errors with ALSO typing errors like `erro XXXXXXXXX`;
- Screaming to the skies and throwing things around will not help;
- Reinstalling SQL Server will not help;
- Reinstalling SQL Server with latest SP will not help;
- Reinstalling OS will not help;
- Reinstalling OS with latest SP will not help;
- Reinstalling <Microsoft Software> with <Any Condition> will not help;
- IIS will not forgive and forget any `;` you put on a asp page, too bad for mainly php coders (eg.: me);
- IIS is for Apache as IE is for Firefox (is this understandable in english? don’t think so);
Another Complaint:
The triumph of a bad software can’t be explained for me, I can’t also understand why they made so much money on something that simply dosen’t work. Maybe users like
things that don’t work? Maybe there’s something magic on crypt errors and blue screens? Maybe Microsoft is paying developers to use their languages and/or software?
Solution:
After several days, tries and lots of time lost I was able to do the migration using MSSQL Enterprise 2005 SP3, the Developer, Express, SQL 2008, SP1 and SP2 gave me crypt errors when copying the database, nothing related to installing the latest SP was on the KB.
Note that i installed ALL versions to try. Thanks microsoft for such great support and programs, looking forward to support the selling of pirated microsoft dvds on streets.
Solution 2:
Move to LAMP, please.
MS SQL 2005 Express per Instance Config Note
by z3n on Sep.02, 2009, under Notes
Problem:
How to run more than one ms sql server version in the same machine?
How to change authentication to MIXED MODE without reinstalling the whole thing?
Solution:
Install with different instances. To connect you will use .\INSTANCENAME or SERVERNAME\INSTANCENAME
To change the authentication method, you need to edit the registry, no wonder it’s another obscure cryptic thing:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\MSSQLServer]
“LoginMode”=dword:00000002
dword 1 means windows authentication, while 2 is the mixed mode, just change it and restart the server.
This key is for MS SQL 2005 EXPRESS.
MSSQL Hell
by z3n on Aug.25, 2009, under Coding, lol
One might say that mssql is more powerfull and versatile than the other free choices around, this person should be slapped in the face repeated times.
MS SQL is the most awful and horrible SQL server ever.
Moving data to a server to another turns into a hell specially when you use the official microsoft tools to do that. Somehow, microsoft managed to put their legacy bugs on this product as well. The default `copy database` tool from SQL Management Studio 2008 comes factory broken. You can’t use it due some unknown mistake. The more i searched about the `Object reference not set to an instance of an object` issue the more crap i found about it. This is also a C# error message, making it harder to find on google, although many people discussed about this specific sql issue, nobody apparently found an fix it.
There’s some patches at microsoft site, but none of them worked.
So, I was tired of SQL Management studio when i found a tool to export/import sql databases, also from microsoft. Despite the timeouts, it worked right…. until, after 3 hours i found out that it didn’t copied the identity columns, which i specificly checked to copy at the program.
Back at Manager, i happly found that you can’t add identity columns without dropping the table and loosing all the data.
So, none of microsoft tools to manage microsoft sql server were able to do a simple database copy, what could be done with a single command line at mysql turns into a hell spiral because of microsoft great tools.
I was tired of it then i decided to look for a third party tool, and i found out MS SQL Maestro, which surprisingly only had a little bug on connection passwords, with this program i was able to do a script of the database, with the identity columns included, and now, at 4am i’m inserting it back on the new database, just hope i don’t fall into another happy error, since client will want his database working at 8am :)
Just easy as that.
MSSQL+PHP = Issues
by z3n on Jul.21, 2009, under Coding
Problem:
On a simple MSSQL (Microsoft SQL Server) with PHP you get this error:
Warning: mssql_query() [function.mssql-query]: message: Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier. (severity 16)
and the best, it dosen’t matter if you convert the data to a different type.P>
Solution:
It looks like that mssql library at php can’t handle the ntext objects right, or at least the way you’re calling it is not right, the ONG>wrong way is:
function _connect_mssql() {
global $_msql,$_msql_id;
if (!$_msql_id) {
if ($_msql=mssql_connect(dbserver,dbuser,dbpass)) {
mssql_select_db(db,$_msql) or die("Unable to select database: ".db);
$_msql_id=1;
} else {
die("Couldn’t connect to database");R> }
$_msql_id=1;
}
}
function _qm($sql) {
global $res,$_msql,$_msql_id;
if (!$_msql_id) { _connect_mssql(); }
$res=mssql_query($sql,$_msql);
if (!$res) { die("Error on query: ".$sql); }
}
Those are simple functions to call it without using a class or too much paraphernalia. And it doesn’t work when the table has a ntext column on it, now a function that works with any type of table:P>
function _connect_mssql() {
global $_msql,$_msql_id;
if (!$_msql_id) {
$_msql=new COM("ADODB.Connection");
$_msql->Open("Provider=SQLOLEDB.1;Password=".dbpass.";Persist Security Info=True;User ID=".dbuser.";Initial Catalog=".db.";Data Source=".dbserver);
$_msql_id=1;
}
}
function _qm($sql) {
global $res,$_msql,$_msql_id;
if (!$_msql_id) { _connect_mssql(); }
$res=$_msql->Execute($sql);
if (!$res) { die("Error on query: ".$sql); }
}
$_msql="";$_msql_id=0;
Note that I used a class and ADODB connection instead, this works prefectly.