Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep data in-row in SQL Server

I am trying to work out how to detect if data in a VARCHAR(n) column in SQL Server 2008 is being stored in-row or off-row. Does anyone know how to do this?

Also, is there a way to keep the data in-row if we want it there?

like image 208
Mr. Flibble Avatar asked Mar 21 '12 12:03

Mr. Flibble


People also ask

How do I show column data in a row in SQL Server?

In SQL Server you can use the PIVOT function to transform the data from rows to columns: select Firstname, Amount, PostalCode, LastName, AccountNumber from ( select value, columnname from yourtable ) d pivot ( max(value) for columnname in (Firstname, Amount, PostalCode, LastName, AccountNumber) ) piv; See Demo.

How do I get data from a specific row in SQL?

To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.

How do I split a single row into multiple rows in SQL Server?

I used STRING_SPLIT() which is a table valued function supports SQL server 2016 and higher versions. You need to provide the formatted string into this function and use cross apply to join and generate the desired output.


Video Answer


2 Answers

To see if a value is in-row or off-row you can use DBCC PAGE

A way to force a VARCHAR(N) column to be in-row (not a VARCHAR(MAX) is to make it part of the clustered index key. This of course limits the length of the field to the maximum index key size of 900.

like image 121
Remus Rusanu Avatar answered Oct 21 '22 21:10

Remus Rusanu


Check out the "large value types out of row" option in SQL Server, if it's a VARCHAR(MAX) column. The documentation is worth reading, because setting the option does not immediately convert the data in the table.

sp_tableoption N'MyTable', 'large value types out of row', 'OFF'
like image 43
Dave Markle Avatar answered Oct 21 '22 20:10

Dave Markle