Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple SQL columns into 1 json column

I am new to querying and using JSON fields. I've done some research, but most of the info I find is regarding querying from JSON to rows and columns. My question is in opposite direction:

Let's say I have a table with 3 columns: (Age, Optin, City). Now i want to add one column in JSON format. I tried:

SELECT Age, Optin, City
INTO #JSON_Table
FROM MyTable
FOR JSON AUTO

Error message: Msg 13602, Level 16, State 1, Line 30 The FOR JSON clause is not allowed in a SELECT INTO statement.

Pretty basic stuff, I know, but I could use some assistence.

like image 279
SQL_M Avatar asked Jun 24 '26 12:06

SQL_M


1 Answers

select * into #JSON_Table
from (SELECT Age, Optin, City
FROM MyTable
FOR JSON PATH
) a(X)

Where X is the column name

like image 185
Harshana Avatar answered Jun 27 '26 06:06

Harshana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!