Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 419855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:49:07+00:00 2026-05-12T18:49:07+00:00

I have a view that I want to be converted into JSON. What is

  • 0

I have a view that I want to be converted into JSON. What is the SQL that I can use to produce on the server the JSON string needed to be returned?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T18:49:07+00:00Added an answer on May 12, 2026 at 6:49 pm
    -- 
    -- Author:      Thiago R. Santos                                           --
    -- Create date: Aug 3rd 2008                                                   --
    -- Description: Returns the contents of a given table                      --
    --              in JavaScript Object Notation.                             --
    -- Params:                                                                 --
    --      @table_name: the table to execute the query                        --
    --      @registries_per_request: equivalent to "select top N * from table" 
    -- 
    --                               replcing N by the actual number           
    -- Influenced by Thomas Frank's post MySQL to JSON @ January 23, 2007      --
    -- Post Url: http://www.thomasfrank.se/mysql_to_json.html                  --
    
    
    
    create procedure [dbo].[GetJSON]
    (
    @table_name varchar(50),
    @registries_per_request smallint = null
    )
    as
    begin
    if((select count(*) from information_schema.tables where table_name =   @table_name)     > 0)
    begin
        declare @json varchar(max),
                @line varchar(max),
                @columns varchar(max),
                @sql nvarchar(max),
                @columnNavigator varchar(50),
                @counter tinyint,
                @size varchar(10)
    
        if (@registries_per_request is null) 
        begin
            set @size = ''
        end
        else 
        begin
            set @size = 'top ' + convert(varchar, @registries_per_request)
        end
        set @columns = '{'
    
        declare schemaCursor cursor
        for select column_name from information_schema.columns where table_name = @table_name
        open    schemaCursor    
    
        fetch next from schemaCursor
        into  @columnNavigator
    
        select  @counter = count(*) from information_schema.columns where table_name = @table_name
    
        while @@fetch_status = 0
        begin
            set @columns = @columns + '''''' + @columnNavigator + ''''':'''''' + convert(varchar, ' + @columnNavigator + ') + '''''''
            set @counter = @counter - 1
            if(0 != @counter) 
            begin
                set @columns = @columns + ','
            end
    
            fetch next from schemaCursor
            into  @columnNavigator
        end 
    
        set @columns =  @columns + '}'
    
        close       schemaCursor
        deallocate  schemaCursor
    
        set @json = '['
    
        set @sql = 'select  ' + @size + '''' + @columns + ''' as json into tmpJsonTable from ' + @table_name
        exec sp_sqlexec @sql
    
        select  @counter = count(*) from tmpJsonTable
    
        declare tmpCur cursor
        for     select * from tmpJsonTable
        open    tmpCur
    
        fetch next from tmpCur
        into  @line
    
        while @@fetch_status = 0
        begin
            set @counter = @counter - 1
            set @json = @json + @line
            if ( 0 != @counter ) 
            begin
                set @json = @json + ','
            end
    
            fetch next from tmpCur
            into  @line
        end
    
        set @json = @json + ']'
    
        close       tmpCur
        deallocate  tmpCur
        drop table  tmpJsonTable
    
        select @json as json
    end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view in SQL Server 2008 that I want to use for
I have a MS SQL view that I want to make available as a
I have an index view that I want to update automatically as the user
I have a partial view that I want to basically take care of itself
I have such view that handles user registration. After creating new user i want
My problem is that I want to have a tab bar view with its
I have a ImageView and draw some things on that view. Now I want
On each cell of my table view I want to have a bar that
I am beginner in rails. I have view that i try to print inside
I have a ArrayList that contains values in the form [ann,john]. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.