Solved

SQL statements with variables

Posted on 2014-01-03
4
263 Views
Last Modified: 2014-01-04
I have a SQL statement that uploads an image file to a FILESTREAM column in SQL server. If I want to introduce variables into this statement, rather than literal values, I understand that I need to produce a dymamic SQL statement, then EXEC that statement. I can't seem to get the syntax right though.


Original (this works ok):

INSERT INTO T_Photos (UI, Filename, Location, Photo)
SELECT newid(), 'myImage.JPG', 'London', Photo.*
FROM OPENROWSET (BULK 'C:\myPath\myImage.JPG', SINGLE_BLOB) Photo


My attempt using variables:

DECLARE
@Filename as varchar(20) = 'myImage.JPG',
@Location as varchar(20) = 'London',
@Filepath as varchar(100) =  'C:\myPath\myImage.jpg',
@UI as uniqueidentifier = NEWID(),
@sql as varchar(1000)

SET @sql =
'INSERT INTO T_Photos (UI, Filename, Location, Photo)' +
'SELECT ' + CONVERT(char(255),@UI) + ', ''' + CONVERT(varchar,@Filename) +''', ''' +
CONVERT(varchar,@Location) + ''', Photo.* ' +
'FROM OPENROWSET (BULK ''' + CONVERT(varchar, @Filepath) + ''', SINGLE_BLOB) Photo'

EXEC @sql
0
Comment
Question by:TimHudspith
  • 2
4 Comments
 
LVL 12

Expert Comment

by:ktaczala
Comment Utility
don't think you need to use the convert
also try using declare just to declare variable and set to assign a value to it.
i.e.
declare @filename varchar(20)
set @filename = "myimage.jpg"


see link:
http://www.sql-server-helper.com/tips/set-vs-select-assigning-variables.aspx
0
 
LVL 6

Expert Comment

by:r3nder
Comment Utility
Here is how I  did it. I used a stored proc to upload because I noticed it would error if I didn't
CREATE PROCEDURE [dbo].[spx_QCUploadFiles]  
                 	-- Add the parameters for the stored procedure here 
                 	@Type varchar(10), 
                 	@filetype varchar(50), 
                 	@filename varchar(150), 
                 	@File_Content_Type varchar(150), 
                 	@Document_File varbinary(MAX), 
                 	@localID int, 
                 	@InspectionUID int 
                 AS 
                 BEGIN 
                 	-- SET NOCOUNT ON added to prevent extra result sets from 
                 	-- interfering with SELECT statements. 
                 	SET NOCOUNT ON; 
                 END	 
                 DECLARE @exFILE int  
                 SET @exFile = (SELECT TOP(1) id FROM [MY_DB].[dbo].[QC_InspectionUploadFiles] WHERE [type] = @type and localID = @localId) 
                 if @exFile > 0 
                 BEGIN 
                 Update [MY_DB].[dbo].[QC_InspectionUploadFiles] 
                 SET [filename] = @filename 
                            ,[filetype] = @filetype 
                            ,[File_Content_Type] = @File_Content_Type  
                            ,[Document_File] = @Document_File 
                            ,[localID] = @localID 
                            ,[Type] = @Type 
                            ,[InspectionUID] = @InspectionUID 
                             WHERE [type] = @type and localID = @localId 
                             SELECT CAST(scope_identity() AS int) 
                 END 

Open in new window

0
 
LVL 6

Accepted Solution

by:
r3nder earned 500 total points
Comment Utility
sorry forgot the insert - here is the insert and update
                 CREATE PROCEDURE [dbo].[spx_QCUploadFiles]  
                 	-- Add the parameters for the stored procedure here 
                 	@Type varchar(10), 
                 	@filetype varchar(50), 
                 	@filename varchar(150), 
                 	@File_Content_Type varchar(150), 
                 	@Document_File varbinary(MAX), 
                 	@localID int, 
                 	@InspectionUID int 
                 AS 
                 BEGIN 
                 	-- SET NOCOUNT ON added to prevent extra result sets from 
                 	-- interfering with SELECT statements. 
                 	SET NOCOUNT ON; 
                 END	 
                 DECLARE @exFILE int  
                 SET @exFile = (SELECT TOP(1) id FROM [MY_DB].[dbo].[QC_InspectionUploadFiles] WHERE [type] = @type and localID = @localId) 
                 if @exFile > 0 
                 BEGIN 
                 Update [MY_DB].[dbo].[QC_InspectionUploadFiles] 
                 SET [filename] = @filename 
                            ,[filetype] = @filetype 
                            ,[File_Content_Type] = @File_Content_Type  
                            ,[Document_File] = @Document_File 
                            ,[localID] = @localID 
                            ,[Type] = @Type 
                            ,[InspectionUID] = @InspectionUID 
                             WHERE [type] = @type and localID = @localId 
                             SELECT CAST(scope_identity() AS int) 
                 END 
                 ELSE 
                 BEGIN 
                     -- Insert statements for procedure here 
                 	INSERT INTO [MY_DB].[dbo].[QC_InspectionUploadFiles] 
                            ([filename] 
                            ,[filetype] 
                            ,[File_Content_Type] 
                            ,[Document_File] 
                            ,[localID] 
                            ,[Type] 
                            ,[InspectionUID]) 
                      VALUES 
                            (@filename 
                            ,@filetype 
                            ,@File_Content_Type 
                            ,@Document_File 
                            ,@localID 
                            ,@Type 
                            ,@InspectionUID) 
                            SELECT CAST(scope_identity() AS int) 
                 END 

Open in new window

0
 

Author Comment

by:TimHudspith
Comment Utility
I needed to do some correction to the convert functions and quote marks to get the syntax right.

'INSERT INTO T_Photos (UI, Filename, Location, Photo) ' +
'SELECT ''' + CONVERT(varchar(100),@UI) + ''', ''' + CONVERT(varchar(20),@Filename) +''', ''' +
CONVERT(varchar (20), @Location) + ''', Photo.* ' +
'FROM OPENROWSET (BULK ''' + CONVERT(varchar, @Filepath) + ''', SINGLE_BLOB) Photo'


In the end I structured the whole thing on the example you provided. Thanks.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now