Insert and Update image field in SQL Server 2008/2005

Few months back I have written one article which was showing how to store image and other files into SQL Server column by converting it to byte object and receive it back. That was with the help of C# but now this time I am going to show, how you can insert and update image or varbinary field from within SQL Server itself.
Before we move further, If you wish to look at my previous article with C# script, please have a look at:
Ok, now let us move ahead with our script in SQL Server itself.
–create table for demonstration
create table emps
(
      name varchar(50),
      dept varchar(10),
      empImg image
)
GO


–insert statement with single_blob to upload image to SQL Server
INSERT INTO emps ([Name],dept,empImg)
SELECT ‘Ritesh’,‘MIS’,
(select * FROM OPENROWSET(BULK ‘C:\Ritesh.JPG’, SINGLE_BLOB) AS img)
GO


–check the inserted data
select * from emps
GO

–update your table, along with image also.
update emps
set empImg=(select * FROM OPENROWSET(BULK ‘C:\Ritesh1.JPG’, SINGLE_BLOB) AS img), dept=‘IT’
where name=‘Ritesh’
GO

–check the data whether it has been updated
select * from emps
go

 
Happy SQLing!!!
Reference: Ritesh Shah
http://www.sqlhub.com
Note: Microsoft Books online is a default reference of all articles but examples and explanations prepared by Ritesh Shah, founder of
http://www.SQLHub.com
Advertisements

Shrink log file for all database in SQL Server 2008/2005

Well, shrinking log file manually and frequently is not at all good idea but we have to do it once in a while when it reaches at very big size, however if it is growing unacceptably and frequently, you have to investigate the reason before shrinking it.

Anyway, our main focus today is how to shrink log file. Well, there is a simple command “DBCC SHRINKFILE” in T-SQL but what, if we want to shrink log file of all database in SQL Server? Well in this situation SP_MsForEachDB (undocumented SP) will come to your help.

Let us see small yet handy script to do so:

use master

DECLARE @SQL varchar (2000)

SELECT @SQL =

SELECT @SQL = @SQL + ‘if ”?” ”master” and ”?” ”msdb” and ”?””tempdb” and ”?” ”model” ‘

SELECT @SQL = @SQL + ‘BEGIN ‘

SELECT @SQL = @SQL + ‘USE ?; ‘

SELECT @SQL = @SQL + ‘SELECT ”?”; ‘

SELECT @SQL = @SQL + ‘DECLARE @LogFile varchar (30); ‘

SELECT @SQL = @SQL + ‘SELECT @LogFile = name FROM dbo.sysfiles WHERE (status & 0x40) 0; ‘

SELECT @SQL = @SQL + ‘dbcc shrinkfile (@LogFile, 1,truncateonly); ‘

SELECT @SQL = @SQL + ‘SELECT fileid, name, filename, size, growth, status, maxsize FROM dbo.sysfiles WHERE (status & 0x40) 0; ‘

SELECT @SQL = @SQL + ‘END’

SELECT @SQL

EXEC sp_MSforeachdb @command1=@SQL

GO

 

To know more about SP_MsForEachDB stored procedure, have a look at one of my previous article at:

http://www.sqlhub.com/2009/03/spmsforeachdb-undocumented-stored.html

http://www.sqlhub.com/2009/04/find-user-in-all-database-with.html

 

Reference: Ritesh Shah
http://www.sqlhub.com
Note: Microsoft Books online is a default reference of all articles but examples and explanations prepared by Ritesh Shah, founder of
http://www.SQLHub.com

Improved INSERT statement in SQL Server 2008

I have written few articles about different types of INSERT statement in SQL Server, SQL Server 2008 came up with many enhancements in T-SQL and developer convenience. There is one enhancement in the way you use INSERT statement.

This new feature called “Raw Constructor” which will be helpful to specify more than one raw in VALUE clause of INSERT statement.

CREATE TABLE emps

(

Name VARCHAR(50),

Dept VARCHAR(10),

Company VARCHAR(15)

)

 

–INSERT records with multiple RAW in VALUE clause

INSERT INTO emps (Name,Dept,Company) VALUES

(‘Ritesh’,‘MIS’,‘chem’),

(‘Bihag’,‘MIS’,‘chem’),

(‘Rajan’,‘acct’,‘mar’)

 

Reference: Ritesh Shah
http://www.sqlhub.com
Note: Microsoft Books online is a default reference of all articles but examples and explanations prepared by Ritesh Shah, founder of
http://www.SQLHub.com