Sample code for 30+ languages & platforms
SQL Server

ETrade List Accounts

See more ETrade Examples

Returns a list of E*TRADE accounts for the current user.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  This requires the Chilkat API to have been previously unlocked.
    --  See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @http, 'OAuth1', 1
    EXEC sp_OASetProperty @http, 'OAuthVerifier', ''
    EXEC sp_OASetProperty @http, 'OAuthConsumerKey', 'ETRADE_CONSUMER_KEY'
    EXEC sp_OASetProperty @http, 'OAuthConsumerSecret', 'ETRADE_CONSUMER_SECRET'

    --  Load the access token previously obtained via the OAuth1 Authorization
    DECLARE @jsonToken int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonToken OUT

    EXEC sp_OAMethod @jsonToken, 'LoadFile', @success OUT, 'qa_data/tokens/etrade.json'
    IF @success <> 1
      BEGIN

        PRINT 'Failed to load OAuth1 token'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @jsonToken
        RETURN
      END

    EXEC sp_OAMethod @jsonToken, 'StringOf', @sTmp0 OUT, 'oauth_token'
    EXEC sp_OASetProperty @http, 'OAuthToken', @sTmp0
    EXEC sp_OAMethod @jsonToken, 'StringOf', @sTmp0 OUT, 'oauth_token_secret'
    EXEC sp_OASetProperty @http, 'OAuthTokenSecret', @sTmp0

    DECLARE @sandboxUrl nvarchar(4000)
    SELECT @sandboxUrl = 'https://apisb.etrade.com/v1/accounts/list'
    DECLARE @liveUrl nvarchar(4000)
    SELECT @liveUrl = 'https://api.etrade.com/v1/accounts/list'

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', @sandboxUrl, @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @jsonToken
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    --  Make sure a successful response was received.
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 >= 300
      BEGIN
        EXEC sp_OAGetProperty @resp, 'StatusLine', @sTmp0 OUT
        PRINT @sTmp0
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0
        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @jsonToken
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 = 204
      BEGIN

        PRINT 'No records available.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @jsonToken
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    --  Sample XML response:

    --  Use this online tool to generate parsing code from sample XML: 
    --  Generate Parsing Code from XML

    --  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    --  <AccountListResponse>
    --      <Accounts>
    --          <Account>
    --              <accountId>82314598</accountId>
    --              <accountIdKey>dBZOKt9xDrtRSAOl4MSiiA</accountIdKey>
    --              <accountMode>IRA</accountMode>
    --              <accountDesc>Brokerage</accountDesc>
    --              <accountName>NickName-1</accountName>
    --              <accountType>MARGIN</accountType>
    --              <institutionType>BROKERAGE</institutionType>
    --              <accountStatus>ACTIVE</accountStatus>
    --              <closedDate>0</closedDate>
    --          </Account>
    --          <Account>
    --              <accountId>58315636</accountId>
    --              <accountIdKey>vQMsebA1H5WltUfDkJP48g</accountIdKey>
    --              <accountMode>BROKERAGE</accountMode>
    --              <accountDesc>Complete Savings</accountDesc>
    --              <accountName>NickName-2</accountName>
    --              <accountType>INDIVIDUAL</accountType>
    --              <institutionType>BROKERAGE</institutionType>
    --              <accountStatus>ACTIVE</accountStatus>
    --              <closedDate>0</closedDate>
    --          </Account>
    --          <Account>
    --              <accountId>70700418</accountId>
    --              <accountIdKey>6_Dpy0rmuQ9cu9IbTfvF2A</accountIdKey>
    --              <accountMode>CASH</accountMode>
    --              <accountDesc>INDIVIDUAL</accountDesc>
    --              <accountName>NickName-3</accountName>
    --              <accountType>INDIVIDUAL</accountType>
    --              <institutionType>BROKERAGE</institutionType>
    --              <accountStatus>ACTIVE</accountStatus>
    --              <closedDate>0</closedDate>
    --          </Account>
    --          <Account>
    --              <accountId>83515143</accountId>
    --              <accountIdKey>xj1Dc18FTqWPqkEEVUr5rw</accountIdKey>
    --              <accountMode>CASH</accountMode>
    --              <accountDesc>INDIVIDUAL</accountDesc>
    --              <accountName/>
    --              <accountType>CASH</accountType>
    --              <institutionType>BROKERAGE</institutionType>
    --              <accountStatus>CLOSED</accountStatus>
    --              <closedDate>1521027780</closedDate>
    --  
    --          </Account>
    --      </Accounts>
    --  </AccountListResponse>

    DECLARE @xml int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT

    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @xml, 'LoadXml', @success OUT, @sTmp0
    EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @accountId int

    DECLARE @accountIdKey nvarchar(4000)

    DECLARE @accountMode nvarchar(4000)

    DECLARE @accountDesc nvarchar(4000)

    DECLARE @accountName nvarchar(4000)

    DECLARE @accountType nvarchar(4000)

    DECLARE @institutionType nvarchar(4000)

    DECLARE @accountStatus nvarchar(4000)

    DECLARE @closedDate int

    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @xml, 'NumChildrenHavingTag', @count_i OUT, 'Accounts|Account'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @xml, 'I', @i
        EXEC sp_OAMethod @xml, 'GetChildIntValue', @accountId OUT, 'Accounts|Account[i]|accountId'
        EXEC sp_OAMethod @xml, 'GetChildContent', @accountIdKey OUT, 'Accounts|Account[i]|accountIdKey'
        EXEC sp_OAMethod @xml, 'GetChildContent', @accountMode OUT, 'Accounts|Account[i]|accountMode'
        EXEC sp_OAMethod @xml, 'GetChildContent', @accountDesc OUT, 'Accounts|Account[i]|accountDesc'
        EXEC sp_OAMethod @xml, 'GetChildContent', @accountName OUT, 'Accounts|Account[i]|accountName'
        EXEC sp_OAMethod @xml, 'GetChildContent', @accountType OUT, 'Accounts|Account[i]|accountType'
        EXEC sp_OAMethod @xml, 'GetChildContent', @institutionType OUT, 'Accounts|Account[i]|institutionType'
        EXEC sp_OAMethod @xml, 'GetChildContent', @accountStatus OUT, 'Accounts|Account[i]|accountStatus'
        EXEC sp_OAMethod @xml, 'GetChildIntValue', @closedDate OUT, 'Accounts|Account[i]|closedDate'
        SELECT @i = @i + 1
      END


    PRINT 'Success.'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @jsonToken
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @xml


END
GO