Articles in the ‘Programming’ Category

SQL Alter a column with a default value

Tuesday, February 9th, 2010

If a column has a default value it means there is a constraint on that column setting it the default value.  The alter command will give you an error like this:

the object is dependent on column ..

I made a script for altering several columns in different tables, I had to alter them from float to decimal, and all of them had default value=0.

DECLARE @col nvarchar(100)
DECLARE @tabel nvarchar(100)
DECLARE @constr nvarchar(100)
declare @sql nvarchar(500)

DECLARE cTemp CURSOR LOCAL FOR select t_obj.name as TABLE_NAME
,col.name as COLUMN_NAME
,c_obj.name as CONSTRAINT_NAME
from sysobjects c_obj
join syscomments com on c_obj.id = com.id
join sysobjects t_obj on c_obj.parent_obj = t_obj.id
join    sysconstraints con on c_obj.id = con.constid
join syscolumns col on t_obj.id = col.id
and con.colid = col.colid where col.name=’col1′ or col.name=’col2′  –etc

OPEN cTemp

FETCH NEXT FROM cTemp INTO @tabel, @col, @constr
WHILE @@FETCH_STATUS = 0
BEGIN

SET @sql = ‘ALTER TABLE ‘+@tabel+’ DROP CONSTRAINT ‘+ @constr
PRINT @sql
EXEC sp_executesql @sql

SET @sql = ‘ALTER TABLE ‘+@tabel+’ ALTER COLUMN ‘+@col+’ DECIMAL(14, 6)’
PRINT @sql
EXEC sp_executesql @sql

SET @sql=’alter table ‘+@tabel+’ ADD CONSTRAINT ‘+@constr+’  DEFAULT 0 FOR ‘+ @col
PRINT @sql
EXEC sp_executesql @sql

FETCH NEXT FROM cTemp INTO @tabel, @col, @constr
END
CLOSE cTemp
DEALLOCATE cTemp

This script will drop the constraints for these columns, alter the columns and then rebuild the constraints. In the select from the cursor you can include the column with the default value, if you need to modify the script for columns with different default values.

Good luck

How to properly save diacritics in MySQL database

Wednesday, January 27th, 2010

To save diacritics in a MySQL database you have 5 steps to follow:

1. Set(create) your database with collation utf8_general_ci;

2. Set(create) your table with utf8_general_ci collation also;

3. Make sure the text fields where you’ll insert diacritics have also the utf8_general_ci collation

4. Convert your string into ‘utf-8′ using any method you like. I recommend using PHP’s multibyte functions

e.g. $my_string = mb_convert_encoding($txt, $charset, mb_detect_encoding($my_string));

where:  $my_string is the string that will be inserted

$charset is thecharset where your diacritics are included.

Check also here the Complete list of Popular character encodings

5. Before the insert query execute this query SET NAMES latin2; (if your diacritics are from one of the latin charsets)

Replacing a text in a word document from VB.net

Monday, January 18th, 2010

This is the code I used:

Dim appWord As New Word.Application
Dim docWord As New Word.Document
docWord = appWord.Documents.Open(“C:\Infofarm\test.doc”)
Try
Dim myStoryRange As Microsoft.Office.Interop.Word.Range
For Each myStoryRange In docWord.StoryRanges
With myStoryRange.Find
.Text = “<-email->”
.Replacement.Text = “aaa@oooo.com”
.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
.Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
End With
Next myStoryRange
docWord.Save()
appWord.Quit()
docWord = Nothing
appWord = Nothing
Catch ex As Exception
MsgBox(ex.Message)
End Try

Hope it help. Good luck!

Backing Up All MySQL Databases

Monday, January 18th, 2010

To backup all the databases in MySQL use:

mysqldump --all-databases -pPASSWORD | bzip2 -c > databasebackup.sql.bz2

The password is for the user Root.

To restore MySQL use:

mysql -pPASSWORD < databasebackup.sql

Setting a culture to an application

Wednesday, January 13th, 2010

To set a Culture (date format, decimal symbol, etc.) simply insert this code to the beginning of the application:

System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo(“en-GB”)

Remember, if you have a multi-threading application, you must insert this line at the beginning of every thread.

Error = [Microsoft][SQL Server Native Client 10.0]Unable to open BCP host data-file

Friday, January 8th, 2010

I had this error when I tried to export some data from SQL Server 2008 with BCP utility:

Error = [Microsoft][SQL Server Native Client 10.0]Unable to open BCP host data-file

The problem was that I tried to export to the path “C:\export.dat”, I tried also to export to “D:\export.dat”, but the error persisted.

The solution to this problem is to set the destination file to “C:\folder\export.dat”.

Al least this worked for me. Hope it helps. Good luck!

Group by minute from a datetime column

Tuesday, January 5th, 2010

GROUP BY convert(varchar(16),datetime column, 120)

Get the name of the month in vb.net in any language

Friday, December 11th, 2009

If you want to get the name of a month in a specific language, you should try this code:

Dim myDTFI As Globalization.DateTimeFormatInfo = New Globalization.CultureInfo(“ro-RO”, True).DateTimeFormat
Console.Write(myDTFI.GetMonthName(2))

myDTFI.GetMonthName(2) will be “februarie”. Hope this helps you.

How to schedule automatic backup in SQL Server?

Saturday, October 24th, 2009

To schedule a database backup operation by using SQL Server Management Studio in SQL Server, follow these steps:

1. Start SQL Server Management Studio.
2. In the Connect to Server dialog box, click the appropriate values in the Server type list, in the Server name list, and in the Authentication list.
3. Click Connect.
4. In Object Explorer, expand Databases.
5. Right-click the database that you want to back up, click Tasks, and then click Back Up.
6. In the Back Up Database – DatabaseName dialog box, type the name of the backup set in the Name box, and then click Add under Destination.
7. In the Select Backup Destination dialog box, type a path and a file name in the Destinations on disk box, and then click OK.
8. In the Script list, click Script Action to Job.
9. In the New Job dialog box, click Steps under Select a page, and then click Edit if you want to change the job parameters.

Note In the Job Step Properties – 1 dialog box, you can see the backup command.
10. Under Select a page, click Schedules, and then click New.
11. In the New Job Schedule dialog box, type the job name in the Name box, specify the job schedule, and then click OK.

Note: If you want to configure alerts or notifications, you can click Alerts or Notifications under Select a page.
12. Click OK two times.

You receive the following message:
The backup of database ‘DatabaseName’ completed successfully.

Note: To verify the backup job, expand SQL Server Agent, and then expand Jobs. When you do this, the SQL Server Agent service must be running.

Links:http://support.microsoft.com/kb/930615

How to detect mobile platforms with PHP?

Friday, October 23rd, 2009

If you seek to detect mobile platforms with PHP you can try the following class:

http://code.google.com/p/php-mobile-detect/