2

I'm tryin to do this:

select * into 'DataBackup'+convert(varchar(10),getdate(),112)+'byMike' from SomeTable 

but it returns an error. I also tried this one but to no avail:

select * into
(select 'DataBackup'+convert(varchar(10),getdate(),112)+'byMike') 
from
SinavSorulari

Basically, I'm trying to make a string that reads: DataBackup20161230byMike and I want to use it with `SELECT * INTO. Can I do that?

Thanks.

Ugur
  • 312
  • 5
  • 15
  • TIP: You're generally better off making an **actual backup**. There can be complications with this kind of "manual" backup if you attempt to "restore" it. To name a few: triggers, identity columns, timestamp/rowversion columns. Certainly, the fact that you're asking about easily including the current date strongly suggests you don't consider your 'manual backup' to be ***extremely*** ad-hoc. Conclusion, you anticipate using it far more often than you should! ***Rather use regular backups.*** – Disillusioned Dec 30 '16 at 12:28

2 Answers2

1

Try this:

declare @query varchar(max)
set @query='select * into DataBackup'+convert(varchar(10),getdate(),112)+'byMike from some_table '
exec ( @query)
Ranjana Ghimire
  • 1,785
  • 1
  • 12
  • 20
0
declare @tablename = 'DatabaseBackup'+convert(varchar(10),getdate(),112)+'byMike'

declare @yoursourcetable = 'Mytable'

declare @selectsql
set @selectsql='select * into '+ @tablename +' from '+@yoursourcetable

sp_executesql ( @selectsql )

I haven't tested it, but should work

d_luffy_de
  • 967
  • 1
  • 9
  • 24