Solved

SQL statements with variables

Posted on 2014-01-03
4
276 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
ID: 39755593
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
ID: 39755631
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
ID: 39755632
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
ID: 39755805
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSIS On fail action 5 37
Convert StartDate-EndDate columns to [DayRange] column 8 29
First Max value 3 27
SQL - Simple Pivot query 8 13
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

839 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