Showing posts with label MS SQL. Show all posts
Showing posts with label MS SQL. Show all posts

Tuesday, December 2, 2014

How to install SQL Server management studio only?

On the download page:
http://www.microsoft.com/en-us/download/details.aspx?id=29062

Select the file just for SQL Server management studio installation:
ENU\x64\SQLManagementStudio_x64_ENU.exe

How to get list of SQL Server instances on local machine?

In command line prompt (Run as admin):
reg query "HKLM\Software\Microsoft\Microsoft SQL Server\Instance Names\SQL"

Wednesday, October 15, 2014

How to check temp table exist in MS SQL Server?

IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
    DROP TABLE #TempTavle

How to write error information into MS SQL Server log?

DECLARE @isExists INT
exec master.dbo.xp_fileexist 'C:\FileName.csv',
@isExists OUTPUT
IF @isExists <> 1
begin
RAISERROR ('Can not find csv file', -- Message text.
16, -- Severity.
1 -- State.
) WITH LOG;
end

How to check exists file in MS SQL server

DECLARE @isExists INT
exec master.dbo.xp_fileexist 'C:\FileName.csv',
@isExists OUTPUT
IF @isExists <> 1
begin
RAISERROR ('Can not find csv file', -- Message text.
16, -- Severity.
1 -- State.
) WITH LOG;
end

Thursday, July 17, 2014

How to use Bulk insert to insert one column in a mutitple columns table?

One of solutions is:
Create a view from that table with only column you want to update.
The use bulk insert:
bulk insert TableView
from 'July17_2014.csv'
with (fieldterminator = ',', rowterminator = '\n')

Wednesday, June 4, 2014

How to kill all current connections to a database in SQL Server ?

In case of you want to restore an database.

There are many ways to do so.
See link on Stackoverflow
http://stackoverflow.com/questions/11620/how-do-you-kill-all-current-connections-to-a-sql-server-2005-database


Or there is another simple way:
1. In SQL Server studio, click Activity Monitor button
2. Filter by database
3. Right click to kill all of connection to that database

Wednesday, May 21, 2014

How to fix problem, MSDTC on server xxx is unavailable?

Following are the steps I did to fix the problem:
- Uninstall: msdtc -uninstall
- Install: msdtc -install
- restarting DTC service;

Do not FORGET
- Restart SQL Server service

Wednesday, November 6, 2013

How to fix "Cannot insert duplicate key in object . The duplicate key value is" problem while insert a new record in SQL Server?

First all, Run:
dbcc checkident([tablename],noreseed)
to check the latest id

Then run
DBCC CHECKIDENT ([tablename], reseed, latest id)
to correct latest id

Friday, July 19, 2013

How to add a field to save current time in SQL Server?


In property window for that culumn:
set the "Default value or binding" getdate()

Friday, May 31, 2013

How to update BLOB field in SQL Server?

UPDATE myTable
SET theBlobField =
(SELECT BulkColumn FROM OPENROWSET (BULK 'C:\logo.png, SINGLE_BLOB) a)
WHERE [ID] = 1

How to save binary (BLOB) field into local file from SQL Server database by SQL?



DECLARE @SQLIMG VARCHAR(MAX),
 @IMG_PATH VARBINARY(MAX),
 @TIMESTAMP VARCHAR(MAX),
 @ObjectToken INT

DECLARE IMGPATH CURSOR FAST_FORWARD FOR 
  SELECT csl_CompanyLogo from mlm_CSCompanySettingsLocalizations
  
OPEN IMGPATH 

FETCH NEXT FROM IMGPATH INTO @IMG_PATH 

WHILE @@FETCH_STATUS = 0
 BEGIN
  SET @TIMESTAMP = 'd:\' + replace(replace(replace(replace(convert(varchar,getdate(),121),'-',''),':',''),'.',''),' ','') + '.bmp'

  PRINT @TIMESTAMP
  PRINT @SQLIMG

  EXEC sp_OACreate 'ADODB.Stream', @ObjectToken OUTPUT
  EXEC sp_OASetProperty @ObjectToken, 'Type', 1
  EXEC sp_OAMethod @ObjectToken, 'Open'
  EXEC sp_OAMethod @ObjectToken, 'Write', NULL, @IMG_PATH
  EXEC sp_OAMethod @ObjectToken, 'SaveToFile', NULL, @TIMESTAMP, 2
  EXEC sp_OAMethod @ObjectToken, 'Close'
  EXEC sp_OADestroy @ObjectToken

  FETCH NEXT FROM IMGPATH INTO @IMG_PATH 
 END 

CLOSE IMGPATH
DEALLOCATE IMGPATH


Reference:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=101754

Wednesday, May 22, 2013

How to insert BLOB data into database by SQL script?

SELECT ThumbnailPhoto.*, null, null, N'tricycle_pink.gif'
FROM OPENROWSET
(BULK 'c:\images\tricycle.jpg', SINGLE_BLOB) ThumbnailPhoto

Thursday, March 14, 2013

How to update tables by inner join in SQL

update L
set l.Text ='something'
from [aTable] a
inner join [bTable] b on a.Id = b.aId
where a.id = 1