Articles in the ‘SQL Server’ 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

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)

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

SQL 2008 install error: ” is not a valid login or you do not have permission.

Wednesday, October 21st, 2009

I got this error when I tried to install SQL Server 2008: ” is not a valid login or you do not have permission.

The problem behind this error is that the name of the computer was the same as the windows username. Change the name of the computer and it will work.

Enable remote connections in sql server

Tuesday, October 6th, 2009

To enable remote connections go to services and there is a process named SQL Server Browser , enable it and then start it. When you install sql server this process is not started by default

How to find out SQL Server version?

Monday, September 7th, 2009

You can find out the SQL Server version using the following queries:

set nocount on
go

select @@version
go

select ‘Edition: ‘ + convert(char(30), serverproperty(‘Edition’))
go

select ‘Product Version: ‘ + convert(char(20), serverproperty(‘ProductVersion’))
go

select ‘Product Level: ‘ + convert(char(20),serverproperty(‘ProductLevel’))
go

set nocount off
go

Clear MS SQL log file

Monday, August 17th, 2009

SP_helpfile

–DBCC ShrinkFile(infocont_apoteca3_log, 50)

To clear a log file, first I suggest to back-up the database and then execute this script, witch will shrink your logfile to 50 MB:

DBCC ShrinkFile(mydatabase_log, 50)

where mydatabase_log is the name of the logfile. You can obtain the name of the log by executing:

SP_helpfile

Repair SQL database from Sql Studio Management

Monday, August 10th, 2009

EXEC sp_resetstatus ‘mydatabase’;
ALTER DATABASE mydatabase SET EMERGENCY
DBCC checkdb(‘mydatabase’)
ALTER DATABASE mydatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DBCC CheckDB (‘mydatabase’, REPAIR_ALLOW_DATA_LOSS)
ALTER DATABASE mydatabase SET MULTI_USER

Select all columns from all tables in the database

Friday, June 19th, 2009

If you want to see all the columns and their properties write the following SQL command in SQL Studio Management:

SELECT * FROM INFORMATION_SCHEMA.COLUMNS

You can filter the results by table name, data type, collation, etc.