Solved

SQL statements with variables

Posted on 2014-01-03
4
268 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

914 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

16 Experts available now in Live!

Get 1:1 Help Now