Solved

SQL statements with variables

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

770 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