Creating a view in tempdb in SQL Server through a post request -


since need send query statement through http post request there limitations. 1. should 1 liner 2. should created in tempdb since going drop afterwards. since sql server takes create view statement in new line feeding new line characters statement. here statement:

declare @newlinechar char(2) = char(13) + char(10);  ('use tempdb;' +@newlinechar + 'go' +@newlinechar +'create view temp_view select name sys.databases')  

this query gives me following error:

msg 102, level 15, state 1. incorrect syntax near 'use tempdb;'. (line 1)

what problem ? thanks

edit: same query works

use tempdb; go  create view temp_view select name sys.databases 

where syntax error?

since sql server takes create view statement in new line feeding new line characters statement.

i have never heard of such requirement. documentation state is: the create view must first statement in query batch.

the statement have in question doesn't make sense. can't drop varchar in ssms , expect sql server execute it.

what want following:

use tempdb; declare @stmt nvarchar(max)=n'create view temp_view select name sys.databases;'; execute sp_executesql @stmt; 

or in 1 line:

use tempdb;declare @stmt nvarchar(max)=n'create view temp_view select name sys.databases;';execute sp_executesql @stmt; 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -