Amazon DynamoDB: Create and Query a Global Secondary Index
See more Amazon DynamoDB Examples
Choose create, status, or query. Create adds DataIndex to the on-demand ExampleItems table: Data is its string partition key and SK its string sort key. The index includes Version in addition to automatically projected table/index keys. Creation/backfill is asynchronous and billable. Status checks DescribeTable once; repeat with a delay and deadline until the index is ACTIVE. Query checks readiness before sending paginated requests.
Items need correctly typed Data and SK attributes to appear in this index. Items missing an index key are omitted (a sparse index). Existing examples already write these attributes. GSI reads are eventually consistent, so recent writes may not appear immediately. GSI queries can retrieve only projected attributes. An index adds storage/write cost; provisioned-mode tables also require appropriate index throughput settings, which this on-demand example intentionally omits.
The item examples use the ExampleItems table from Create a Table, with string partition key PK and string sort key SK. Wait for ACTIVE. DynamoDB AttributeValue JSON uses wrappers such as {"S":"text"}, {"N":"123.45"}, and {"BOOL":true}. Numeric attributes travel as strings; preserve their precision rather than converting them to int or floating point. Names and IDs remain strings.
Uses DynamoDB's regional JSON API: POST to /, application/x-amz-json-1.0, and an operation-specific X-Amz-Target: DynamoDB_20120810... header. The 20120810 API identifier is still current. Chilkat.Rest and AuthAws sign each request with AWS Signature Version 4, service dynamodb. Set the region once; the example builds the matching commercial AWS endpoint. Other AWS partitions use their own endpoint suffixes.
Read current IAM credentials from a private qa_data/aws/dynamodb.json containing string fields access_key, secret_key, and, for temporary credentials, session_token. Prefer short-lived role credentials and refresh them before expiry. This code explicitly reads the file; it does not discover an AWS profile or refresh credentials automatically. The session token is included in the signed X-Amz-Security-Token query parameter, so keep request URLs and diagnostics private. A Socket explicitly enables TLS certificate verification and is supplied to Rest. Reuse the configured Rest object for subsequent requests.
IAM permissions for the illustrated actions: dynamodb:UpdateTable, dynamodb:DescribeTable, dynamodb:Query. Limit access to the appropriate table, index, backup, and restore-target resources. Restoring customer-managed-key encrypted data can also require KMS permissions.
Checks transport completion, HTTP 200, and JSON validity separately. DynamoDB errors include __type and message. HTTP 400 can mean throttling, failed conditions, invalid input, or expired credentials. Use bounded exponential backoff with jitter for retryable failures; renew expired credentials. A failed condition requires an application decision, not an automatic retry. For writes with an uncertain outcome, read/reconcile before retrying. Only the explicitly described batch retries are automatic; other writes are not automatically replayed.
UpdateTable reference, DescribeTable reference, Query reference
Chilkat SQL Server Downloads
-- 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 @sTmp1 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
SELECT @success = 0
-- Private configuration, not a file in public web content or source control.
-- Load current IAM credentials authorized for the DynamoDB operation.
DECLARE @credentials int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @credentials OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @credentials, 'Strict', 1
EXEC sp_OAMethod @credentials, 'LoadFile', @success OUT, 'qa_data/aws/dynamodb.json'
IF @success <> 1
BEGIN
PRINT 'Could not load private AWS credentials.'
EXEC @hr = sp_OADestroy @credentials
RETURN
END
EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'access_key'
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid access_key.'
EXEC @hr = sp_OADestroy @credentials
RETURN
END
EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'secret_key'
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid secret_key.'
EXEC @hr = sp_OADestroy @credentials
RETURN
END
-- Set this once to the region containing the table.
DECLARE @region nvarchar(4000)
SELECT @region = 'us-west-2'
DECLARE @endpoint int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 'dynamodb.'
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @region
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.amazonaws.com'
DECLARE @aws int
EXEC @hr = sp_OACreate 'Chilkat.AuthAws', @aws OUT
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
EXEC sp_OASetProperty @aws, 'AccessKey', @sTmp0
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
EXEC sp_OASetProperty @aws, 'SecretKey', @sTmp0
EXEC sp_OASetProperty @aws, 'Region', @region
EXEC sp_OASetProperty @aws, 'ServiceName', 'dynamodb'
EXEC sp_OASetProperty @aws, 'SignatureVersion', 4
DECLARE @rest int
EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
EXEC sp_OASetProperty @rest, 'ConnectTimeoutMs', 20000
EXEC sp_OASetProperty @rest, 'IdleTimeoutMs', 60000
EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @aws
IF @success <> 1
BEGIN
PRINT 'Could not configure AWS signing.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
RETURN
END
-- Temporary IAM/STS credentials require their session token on every signed request.
-- AddQueryParam includes it in SigV4's canonical query. Never log the request URL.
-- Leave session_token absent or empty only for credentials that do not have one.
EXEC sp_OAMethod @credentials, 'HasMember', @iTmp0 OUT, 'session_token'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'session_token'
IF @iTmp0 <> 1
BEGIN
PRINT 'session_token must be a string.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
RETURN
END
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
IF @sTmp0 <> ''
BEGIN
EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'X-Amz-Security-Token', @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not set the session-token parameter.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
RETURN
END
END
END
-- Supply a Socket to explicitly enable TLS certificate verification.
DECLARE @connection int
EXEC @hr = sp_OACreate 'Chilkat.Socket', @connection OUT
EXEC sp_OASetProperty @connection, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @connection, 'MaxReadIdleMs', 60000
EXEC sp_OASetProperty @connection, 'MaxSendIdleMs', 60000
EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @connection, 'Connect', @success OUT, @sTmp0, 443, 1, 20000
IF @success <> 1
BEGIN
PRINT 'TLS connection failed; inspect diagnostics privately.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
RETURN
END
EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 1
IF @success <> 1
BEGIN
PRINT 'Could not attach the TLS connection.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
RETURN
END
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/x-amz-json-1.0'
IF @success <> 1
BEGIN
PRINT 'Could not set the content type.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
RETURN
END
DECLARE @body int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @body OUT
DECLARE @result int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT
EXEC sp_OASetProperty @result, 'Strict', 1
DECLARE @requestBody int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @requestBody OUT
DECLARE @responseBody int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @responseBody OUT
DECLARE @errorType int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @errorType OUT
DECLARE @tableName nvarchar(4000)
SELECT @tableName = 'ExampleItems'
-- Choose "create", "status", or "query". Start with status on an existing table.
DECLARE @action nvarchar(4000)
SELECT @action = 'status'
IF (@action <> 'create') and (@action <> 'status') and (@action <> 'query')
BEGIN
PRINT 'Choose create, status, or query.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
IF @action = 'create'
BEGIN
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'X-Amz-Target', 'DynamoDB_20120810.UpdateTable'
IF @success <> 1
BEGIN
PRINT 'Could not set the DynamoDB operation.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'TableName', @tableName
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'AttributeDefinitions[0].AttributeName', 'Data'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'AttributeDefinitions[0].AttributeType', 'S'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'AttributeDefinitions[1].AttributeName', 'SK'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'AttributeDefinitions[1].AttributeType', 'S'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'GlobalSecondaryIndexUpdates[0].Create.IndexName', 'DataIndex'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'GlobalSecondaryIndexUpdates[0].Create.KeySchema[0].AttributeName', 'Data'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'GlobalSecondaryIndexUpdates[0].Create.KeySchema[0].KeyType', 'HASH'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'GlobalSecondaryIndexUpdates[0].Create.KeySchema[1].AttributeName', 'SK'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'GlobalSecondaryIndexUpdates[0].Create.KeySchema[1].KeyType', 'RANGE'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'GlobalSecondaryIndexUpdates[0].Create.Projection.ProjectionType', 'INCLUDE'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'GlobalSecondaryIndexUpdates[0].Create.Projection.NonKeyAttributes[0]', 'Version'
-- EmitSb appends. Clear the request buffer before each request/page.
EXEC sp_OAMethod @requestBody, 'Clear', NULL
EXEC sp_OAMethod @body, 'EmitSb', @success OUT, @requestBody
IF @success <> 1
BEGIN
PRINT 'Could not encode the request.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', '/', @requestBody, @responseBody
IF @success <> 1
BEGIN
PRINT 'Request did not complete; inspect diagnostics privately.'
-- A write may already have succeeded. Reconcile its outcome before retrying.
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
PRINT 'DynamoDB HTTP status: ' + @iTmp0
EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
IF @success = 1
BEGIN
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, '__type'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @errorType, 'Clear', NULL
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
EXEC sp_OAMethod @errorType, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @errorType, 'GetAsString', @sTmp0 OUT
PRINT 'AWS error type: ' + @sTmp0
END
END
-- Throttling can be HTTP 400. Inspect __type, not just the HTTP status.
-- Keep full error messages, item data, and signed request diagnostics private.
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
IF @success <> 1
BEGIN
PRINT 'Expected a well-formed JSON response.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'TableDescription.TableStatus'
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'TableDescription.TableStatus'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid TableDescription.TableStatus.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
PRINT 'Index creation accepted. Run status until DataIndex is ACTIVE.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'X-Amz-Target', 'DynamoDB_20120810.DescribeTable'
IF @success <> 1
BEGIN
PRINT 'Could not set the DynamoDB operation.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'TableName', @tableName
-- EmitSb appends. Clear the request buffer before each request/page.
EXEC sp_OAMethod @requestBody, 'Clear', NULL
EXEC sp_OAMethod @body, 'EmitSb', @success OUT, @requestBody
IF @success <> 1
BEGIN
PRINT 'Could not encode the request.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', '/', @requestBody, @responseBody
IF @success <> 1
BEGIN
PRINT 'Request did not complete; inspect diagnostics privately.'
-- A write may already have succeeded. Reconcile its outcome before retrying.
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
PRINT 'DynamoDB HTTP status: ' + @iTmp0
EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
IF @success = 1
BEGIN
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, '__type'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @errorType, 'Clear', NULL
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
EXEC sp_OAMethod @errorType, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @errorType, 'GetAsString', @sTmp0 OUT
PRINT 'AWS error type: ' + @sTmp0
END
END
-- Throttling can be HTTP 400. Inspect __type, not just the HTTP status.
-- Keep full error messages, item data, and signed request diagnostics private.
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
IF @success <> 1
BEGIN
PRINT 'Expected a well-formed JSON response.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'Table.TableStatus'
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Table.TableStatus'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid Table.TableStatus.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
DECLARE @indexStatus nvarchar(4000)
SELECT @indexStatus = ''
DECLARE @indexPosition int
SELECT @indexPosition = 0
EXEC sp_OAMethod @result, 'SizeOfArray', @iTmp0 OUT, 'Table.GlobalSecondaryIndexes'
WHILE (@indexPosition < @iTmp0)
BEGIN
EXEC sp_OASetProperty @result, 'I', @indexPosition
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Table.GlobalSecondaryIndexes[i].IndexName'
IF @sTmp0 = 'DataIndex'
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @indexStatus OUT, 'Table.GlobalSecondaryIndexes[i].IndexStatus'
END
SELECT @indexPosition = @indexPosition + 1
END
PRINT 'DataIndex status (empty means absent): ' + @indexStatus
IF @action = 'status'
BEGIN
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
IF @indexStatus <> 'ACTIVE'
BEGIN
PRINT 'The index is not ACTIVE; no query sent.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
EXEC sp_OAMethod @body, 'Clear', NULL
EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'X-Amz-Target', 'DynamoDB_20120810.Query'
IF @success <> 1
BEGIN
PRINT 'Could not set the DynamoDB operation.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
RETURN
END
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'TableName', @tableName
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'IndexName', 'DataIndex'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'KeyConditionExpression', '#data = :value'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'ExpressionAttributeNames.#data', 'Data'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'ExpressionAttributeValues.:value.S', 'Initial value'
EXEC sp_OAMethod @body, 'UpdateBool', @success OUT, 'ConsistentRead', 0
EXEC sp_OAMethod @body, 'UpdateInt', @success OUT, 'Limit', 100
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'ReturnConsumedCapacity', 'INDEXES'
DECLARE @lastKey int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @lastKey OUT
DECLARE @startKey int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @startKey OUT
DECLARE @seenKeys int
EXEC @hr = sp_OACreate 'Chilkat.Hashtable', @seenKeys OUT
DECLARE @more int
SELECT @more = 1
DECLARE @page int
SELECT @page = 0
WHILE (@more = 1)
BEGIN
IF @page = 1000
BEGIN
PRINT 'Stopped at 1000 pages; index query is incomplete. Save ExclusiveStartKey to resume.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
EXEC @hr = sp_OADestroy @lastKey
EXEC @hr = sp_OADestroy @startKey
EXEC @hr = sp_OADestroy @seenKeys
RETURN
END
-- EmitSb appends. Clear the request buffer before each request/page.
EXEC sp_OAMethod @requestBody, 'Clear', NULL
EXEC sp_OAMethod @body, 'EmitSb', @success OUT, @requestBody
IF @success <> 1
BEGIN
PRINT 'Could not encode the request.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
EXEC @hr = sp_OADestroy @lastKey
EXEC @hr = sp_OADestroy @startKey
EXEC @hr = sp_OADestroy @seenKeys
RETURN
END
EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', '/', @requestBody, @responseBody
IF @success <> 1
BEGIN
PRINT 'Request did not complete; inspect diagnostics privately.'
-- A write may already have succeeded. Reconcile its outcome before retrying.
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
EXEC @hr = sp_OADestroy @lastKey
EXEC @hr = sp_OADestroy @startKey
EXEC @hr = sp_OADestroy @seenKeys
RETURN
END
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
PRINT 'DynamoDB HTTP status: ' + @iTmp0
EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
IF @success = 1
BEGIN
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, '__type'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @errorType, 'Clear', NULL
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
EXEC sp_OAMethod @errorType, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @errorType, 'GetAsString', @sTmp0 OUT
PRINT 'AWS error type: ' + @sTmp0
END
END
-- Throttling can be HTTP 400. Inspect __type, not just the HTTP status.
-- Keep full error messages, item data, and signed request diagnostics private.
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
EXEC @hr = sp_OADestroy @lastKey
EXEC @hr = sp_OADestroy @startKey
EXEC @hr = sp_OADestroy @seenKeys
RETURN
END
EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
IF @success <> 1
BEGIN
PRINT 'Expected a well-formed JSON response.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
EXEC @hr = sp_OADestroy @lastKey
EXEC @hr = sp_OADestroy @startKey
EXEC @hr = sp_OADestroy @seenKeys
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'Items'
IF @iTmp0 <> 4
BEGIN
PRINT 'Expected Items array.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
EXEC @hr = sp_OADestroy @lastKey
EXEC @hr = sp_OADestroy @startKey
EXEC @hr = sp_OADestroy @seenKeys
RETURN
END
DECLARE @i int
SELECT @i = 0
EXEC sp_OAMethod @result, 'SizeOfArray', @iTmp0 OUT, 'Items'
WHILE (@i < @iTmp0)
BEGIN
EXEC sp_OASetProperty @result, 'I', @i
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'Items[i].PK.S'
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Items[i].PK.S'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid Items[i].PK.S.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
EXEC @hr = sp_OADestroy @lastKey
EXEC @hr = sp_OADestroy @startKey
EXEC @hr = sp_OADestroy @seenKeys
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'Items[i].SK.S'
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Items[i].SK.S'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid Items[i].SK.S.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
EXEC @hr = sp_OADestroy @lastKey
EXEC @hr = sp_OADestroy @startKey
EXEC @hr = sp_OADestroy @seenKeys
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Items[i].PK.S'
EXEC sp_OAMethod @result, 'StringOf', @sTmp1 OUT, 'Items[i].SK.S'
PRINT 'PK: ' + @sTmp0 + '; SK: ' + @sTmp1
SELECT @i = @i + 1
END
EXEC sp_OAMethod @result, 'SizeOfArray', @iTmp0 OUT, 'Items'
PRINT 'Items returned in this page: ' + @iTmp0
EXEC sp_OAMethod @result, 'HasMember', @iTmp0 OUT, 'ConsumedCapacity.CapacityUnits'
IF @iTmp0 = 1
BEGIN
-- Capacity can be fractional; do not truncate it with IntOf.
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'ConsumedCapacity.CapacityUnits'
PRINT 'Consumed capacity: ' + @sTmp0
END
SELECT @page = @page + 1
SELECT @more = 0
-- Empty Items does not imply completion; filtering can remove an entire page.
EXEC sp_OAMethod @result, 'HasMember', @iTmp0 OUT, 'LastEvaluatedKey'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'LastEvaluatedKey'
IF @iTmp0 <> 3
BEGIN
PRINT 'Expected LastEvaluatedKey object.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
EXEC @hr = sp_OADestroy @lastKey
EXEC @hr = sp_OADestroy @startKey
EXEC @hr = sp_OADestroy @seenKeys
RETURN
END
EXEC sp_OAMethod @result, 'ObjectOf2', @success OUT, 'LastEvaluatedKey', @lastKey
IF @success <> 1
BEGIN
PRINT 'Could not read the continuation key.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
EXEC @hr = sp_OADestroy @lastKey
EXEC @hr = sp_OADestroy @startKey
EXEC @hr = sp_OADestroy @seenKeys
RETURN
END
EXEC sp_OAGetProperty @lastKey, 'Size', @iTmp0 OUT
IF @iTmp0 > 0
BEGIN
-- Preserve the entire typed key map. Sort members only to detect repeated keys,
-- even if a response changes JSON member order.
EXEC sp_OAMethod @lastKey, 'Sort', NULL, 1, 1
EXEC sp_OASetProperty @lastKey, 'EmitCompact', 1
DECLARE @keyText nvarchar(4000)
EXEC sp_OAMethod @lastKey, 'Emit', @keyText OUT
EXEC sp_OAMethod @seenKeys, 'Contains', @iTmp0 OUT, @keyText
IF @iTmp0 = 1
BEGIN
PRINT 'Repeated query key; index query is incomplete.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
EXEC @hr = sp_OADestroy @lastKey
EXEC @hr = sp_OADestroy @startKey
EXEC @hr = sp_OADestroy @seenKeys
RETURN
END
EXEC sp_OAMethod @seenKeys, 'AddInt', @success OUT, @keyText, 1
IF @success <> 1
BEGIN
PRINT 'Could not track query pages.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
EXEC @hr = sp_OADestroy @lastKey
EXEC @hr = sp_OADestroy @startKey
EXEC @hr = sp_OADestroy @seenKeys
RETURN
END
EXEC sp_OAMethod @body, 'UpdateNewObject', @success OUT, 'ExclusiveStartKey'
IF @success <> 1
BEGIN
PRINT 'Could not prepare ExclusiveStartKey.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
EXEC @hr = sp_OADestroy @lastKey
EXEC @hr = sp_OADestroy @startKey
EXEC @hr = sp_OADestroy @seenKeys
RETURN
END
EXEC sp_OAMethod @body, 'ObjectOf2', @success OUT, 'ExclusiveStartKey', @startKey
IF @success <> 1
BEGIN
PRINT 'Could not access ExclusiveStartKey.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
EXEC @hr = sp_OADestroy @lastKey
EXEC @hr = sp_OADestroy @startKey
EXEC @hr = sp_OADestroy @seenKeys
RETURN
END
EXEC sp_OAMethod @startKey, 'Load', @success OUT, @keyText
IF @success <> 1
BEGIN
PRINT 'Could not copy the full continuation key.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
EXEC @hr = sp_OADestroy @lastKey
EXEC @hr = sp_OADestroy @startKey
EXEC @hr = sp_OADestroy @seenKeys
RETURN
END
SELECT @more = 1
END
END
END
PRINT 'Index query complete.'
EXEC @hr = sp_OADestroy @credentials
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @aws
EXEC @hr = sp_OADestroy @rest
EXEC @hr = sp_OADestroy @connection
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @requestBody
EXEC @hr = sp_OADestroy @responseBody
EXEC @hr = sp_OADestroy @errorType
EXEC @hr = sp_OADestroy @lastKey
EXEC @hr = sp_OADestroy @startKey
EXEC @hr = sp_OADestroy @seenKeys
END
GO