-1

We store several sql statements in one of our tables. For example, the following SQL statement will be stored in [Validations].[SqlSource]:

select 
VendorId,
VendorName,
VendorAddress,
VendorCity,
VendorState,
VendorZip
from Vendor

The problem is that when we copy this SQL directly from the table, it'll lose the carriage returns. This means that pasting the sql will look like this:

select VendorId, VendorName, VendorAddress, VendorCity, VendorState, VendorZip from Vendor

It's not an issue with this sql since it's short, but when the SQL is long, then the string when pasting will be long.

Is there a way to keep the formatting when I copy the string and paste to a text editor?

I added an example of how it's inserted into the table:

create table toerase_SQL
(
    SqlSource varchar(max)
)
insert into toerase_SQL 
select   
'select '+
'VendorId, '+
'VendorName, '+
'VendorAddress, '+
'VendorCity, '+
'VendorState, '+
'VendorZip '+
'from Vendor '

select * from toerase_SQL
fdkgfosfskjdlsjdlkfsf
  • 3,165
  • 2
  • 43
  • 110

1 Answers1

3

When you concatenate strings with something like

SELECT
'a' +
'b' +
'c'

it means, take the string 'a' and append the string 'b' and then append the string 'c', so the result is 'abc'. It does not automatically add line-breaks (nor spaces) that happen to be near the plus/concatenation operator.

It is the same as

SELECT
'a'+'b'+'c'

You will need to include the line-breaks in the string in order to select them.

For example,

SELECT
'a
b
c'

has the line-breaks captured within the string itself.

Or you could add line-breaks explicitly, like

SELECT
'a' + CHAR(13)+CHAR(10)+
'b' + CHAR(13)+CHAR(10)+
'c'

You can see both of these methods working in this Fiddle.

EdmCoff
  • 3,506
  • 1
  • 9
  • 9