SQLTeam.com | Weblogs | Forums

How to transfer a query result from a tab to another one without going through a system file

Hi guys, I have a question.
I need to transfer a query's result from a tab to another tab without having it saved on disc.

For example, i want to transfer the query's result from #FIRST_TABLE to #RET_TABLE, through a variable or whatever

IF OBJECT_ID('TEMPDB..#FIRST_TABLE') IS NOT NULL DROP TABLE #FIRST_TABLE
IF OBJECT_ID('TEMPDB..#RET_TABLE') IS NOT NULL DROP TABLE #RET_TABLE
CREATE TABLE #FIRST_TABLE(ROW_ONE INT, TEXTON2 VARCHAR(100))
CREATE TABLE #RET_TABLE(FILENAME VARCHAR(255), FILEBINARY VARBINARY(MAX))

DECLARE @VARBINARY VARBINARY(MAX), @VARFILENAME VARCHAR(255)

INSERT INTO #FIRST_TABLE VALUES
(1, 'ROW_ONE'),
(2, 'ROW_TWO'),
(3, 'ROW_THREE'),
(4, 'ROW_FOUR'),
(5, 'ROW_FIVE'),
(6, 'ROW_SIX')

SET @VARFILENAME = 'PIPPO.CSV'-- the format is not important as long as the type is known
SET @VARBINARY = ???? -- this is supposed to be the result of the query "SELECT * FROM #FIRST_TABLE"

INSERT INTO #RET_TABLE VALUES
(@VARFILENAME, @VARBINARY)

DROP TABLE #FIRST_TABLE
--DROP TABLE #RET_TABLE

-- from #RET_TABLE i want to recover the result from a binary field into a file

hi

please use
select * into

example
#abc
select * into #def from #abc

hope it helps
:slight_smile: :slight_smile:

i love any feedback
thanks

1 Like

Thanks, but that's not what I thought I'd do.
I would like to insert the result of the query in a column of the second table.

similar

SET @VARFILENAME = 'PIPPO.CSV'-- the format is not important as long as the type is known
SET @VARBINARY = ???? -- this is supposed to be the result of the query "SELECT * FROM #FIRST_TABLE"

INSERT INTO #RET_TABLE VALUES
(@VARFILENAME, @VARBINARY)

thanks in advance

hi

i took another stab at this

does this help ???

drop create data ...
IF OBJECT_ID('TEMPDB..#FIRST_TABLE') IS NOT NULL 
DROP TABLE #FIRST_TABLE

IF OBJECT_ID('TEMPDB..#RET_TABLE') IS NOT NULL 
DROP TABLE #RET_TABLE


CREATE TABLE #FIRST_TABLE(ROW_ONE INT, TEXTON2 VARCHAR(100))
CREATE TABLE #RET_TABLE(FILENAME VARCHAR(255), FILEBINARY VARBINARY(MAX))

DECLARE @VARFILENAME VARCHAR(255)

INSERT INTO #FIRST_TABLE VALUES
(1, 'ROW_ONE'),
(2, 'ROW_TWO'),
(3, 'ROW_THREE'),
(4, 'ROW_FOUR'),
(5, 'ROW_FIVE'),
(6, 'ROW_SIX')
go
SQL ..
SET @VARFILENAME = 'PIPPO.CSV' *-- the format is not important as long as the type is known*  

INSERT INTO #ret_table 
SELECT @VARFILENAME, 
Cast (row_one AS  VARBINARY) 
+  Cast(texton2 AS  VARBINARY) 
FROM #first_table 

SELECT * 
FROM #ret_table 

go
Results...

image