Solved

SQL statements with variables

Posted on 2014-01-03
4
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

739 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