2

I'm trying to consume a rest/json get api via CLR stored procedure. My problem is that i'm getting only one(last) record.

Here is my code:

public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void FrtApiJsonGet ()
    {

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("some site");

        var byteArray = Encoding.ASCII.GetBytes("login:password");

        request.Method = "GET";
        request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + 
                Convert.ToBase64String(byteArray));

        using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (Stream receiveStream = response.GetResponseStream())
            {
                using (StreamReader readStream = new StreamReader(receiveStream,
                  Encoding.UTF8))
                {
                    string strContent = readStream.ReadToEnd();

                    string value1 = string.Empty;
                    string value2 = string.Empty;
                    string value3 = string.Empty;
                    string value4 = string.Empty;


                    SqlPipe pipe = SqlContext.Pipe;
                    SqlMetaData[] cols = new SqlMetaData[4];
                    cols[0] = new SqlMetaData("value1", SqlDbType.NVarChar, 50);
                    cols[1] = new SqlMetaData("value2", SqlDbType.NVarChar, 50);
                    cols[2] = new SqlMetaData("value3", SqlDbType.NVarChar, 50);
                    cols[3] = new SqlMetaData("value4", SqlDbType.NVarChar, 50);

                    SqlDataRecord record = new SqlDataRecord(cols);
                    pipe.SendResultsStart(record);

                    var strArray = strContent.Split(new string[] { "{,}" },
                                      StringSplitOptions.None);

                    foreach(var str in strArray)
                    {
                        var subArray = str.Split(',');
                        foreach (var substr in subArray)
                        {
                            if (substr.Contains("\"value1\""))
                                value1 = substr.Split('\"')[3];

                            if (substr.Contains("\"value2\""))
                                callKey = substr.Split('\"')[3];

                            if (substr.Contains("\"value3\""))
                                callPhase = substr.Split('\"')[3];

                            if (substr.Contains("\"value4\""))
                                callresult = substr.Split('\"')[3];
                        }
                        record.SetSqlString(0, new SqlString(value1));
                        record.SetSqlString(1, new SqlString(value2));
                        record.SetSqlString(2, new SqlString(value3));
                        record.SetSqlString(3, new SqlString(value4));

                        pipe.SendResultsRow(record);


                        value1 = string.Empty;
                        value2 = string.Empty;
                        value3 = string.Empty;
                        value4 = string.Empty;
                    }

                    pipe.SendResultsEnd();

                }
            }
        }

    }
}

I know that the api works and if i try the same code without the sqlpipe in console app it returns a list of values...Yet when used as clr stored procedure i get only one(last) record.

4
  • at first glance it looks correct. there might be an issue with your loops and how you are assigning the values. Include a pipe.Send(str); just before the var subArray = str.Split(','); so you can see what should be each row. Commented Jun 21, 2018 at 13:11
  • Thanks! The problem is in this part: var strArray = strContent.Split(new string[] { "{,}" },StringSplitOptions.None); , string is not splitting here -but i dont know why. Commented Jun 21, 2018 at 13:31
  • Do you actually have delimiters in the form of {,}? perhaps there is an encoding issue (doubtful, but have to ask). Perhaps there is a "hidden" character in the incoming data that makes it not match exactly to {,}? I will post an answer since the overall issue has been identified. Commented Jun 21, 2018 at 13:38
  • This should be fairly easy to debug. Create a console app where you have a method mimicking FrtApiJsonGet but without the SqlPipe, SqlContext and all that stuff. You just loop strArray and see what you get. Commented Jun 21, 2018 at 16:31

1 Answer 1

1

The overall structure appears to be correct, in terms of the handling of SendResultsStart, SendResultsRow, and SendResultsEnd.

The next place to check is in the loop itself. Quite likely the initial Split is not happening as you believe it should be. You should include a pipe.Send(str); just before the var subArray = str.Split(','); so you can see how the response is being chopped up into rows. This will send each chunk to the "Messages" tab so that you can debug it (same as doing PRINT via T-SQL).

Sign up to request clarification or add additional context in comments.

1 Comment

I feel like a complete ass, the problem was truly in the first split - var strArray = strContent.Split(new string[] { "{,}" },StringSplitOptions.None) - the problem was a typo, split should have been { "},{" } and when i was debuging this problem in console app i fixed it there but not in an actual code for clr stored procedure. THANKS ! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.