You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求指导:修改SQL游标以将全NULL列信息(表名+列名)存入表

Solution to Store Null-Only Columns in a Table

Got it, let's get this sorted for you! The core issue with your original code is that it only prints the null-only columns—we'll adjust it to insert those column (and table) names into a dedicated results table instead. Here's a step-by-step fix:

1. First, Create a Table to Store Results

First, let's set up a table to hold the names of tables and their all-null columns. Run this once:

CREATE TABLE NullOnlyColumns (
    TableName VARCHAR(255) NOT NULL,
    ColumnName VARCHAR(255) NOT NULL,
    PRIMARY KEY (TableName, ColumnName) -- Optional: prevents duplicate entries
);

2. Modified Cursor Code (With Insert Logic)

We'll tweak your existing cursor to replace the PRINT statement with an INSERT, plus add a variable for the table name to make it easier to reuse later:

DECLARE @col VARCHAR(255), @cmd VARCHAR(MAX), @targetTable VARCHAR(255) = 'Account'

-- Optional: Clear previous results before running (uncomment if needed)
-- TRUNCATE TABLE NullOnlyColumns;

DECLARE getinfo CURSOR FOR
SELECT c.name 
FROM sys.tables t 
JOIN sys.columns c ON t.Object_ID = c.Object_ID 
WHERE t.Name = @targetTable

OPEN getinfo
FETCH NEXT FROM getinfo INTO @col

WHILE @@FETCH_STATUS = 0
BEGIN
    -- Build dynamic SQL to check if the column is all-null, then insert to results table
    SELECT @cmd = 'IF NOT EXISTS (SELECT TOP 1 * FROM ' + QUOTENAME(@targetTable) + ' WHERE [' + @col + '] IS NOT NULL) 
                   BEGIN 
                       INSERT INTO NullOnlyColumns (TableName, ColumnName)
                       VALUES (''' + @targetTable + ''', ''' + @col + ''')
                   END'
    EXEC(@cmd)
    
    FETCH NEXT FROM getinfo INTO @col
END

CLOSE getinfo
DEALLOCATE getinfo

Key Changes Made:

  • Added a @targetTable variable so you can easily switch to other tables later
  • Replaced the PRINT statement with an INSERT to store results in NullOnlyColumns
  • Used QUOTENAME() around the table name to avoid errors if your table has special characters or matches SQL keywords
  • Added an optional TRUNCATE to clear old results before each run

3. Bonus: Faster Non-Cursor Alternative

Cursors can be slow for large tables. If you prefer a more efficient approach, use dynamic SQL to generate all check statements in one go:

DECLARE @targetTable VARCHAR(255) = 'Account'
DECLARE @batchSql VARCHAR(MAX) = ''

-- Optional: Clear previous results
-- TRUNCATE TABLE NullOnlyColumns;

-- Build a single batch of SQL to check all columns
SELECT @batchSql = @batchSql + 
'IF NOT EXISTS (SELECT TOP 1 * FROM ' + QUOTENAME(@targetTable) + ' WHERE [' + c.name + '] IS NOT NULL)
    INSERT INTO NullOnlyColumns (TableName, ColumnName) VALUES (''' + @targetTable + ''', ''' + c.name + ''');
'
FROM sys.tables t 
JOIN sys.columns c ON t.Object_ID = c.Object_ID 
WHERE t.Name = @targetTable

-- Execute the batch
EXEC(@batchSql)

Once you run either version, you can query the results with:

SELECT * FROM NullOnlyColumns;

This will give you the exact list of columns to exclude when using dbAMP to upload to Salesforce.

内容的提问来源于stack exchange,提问作者Mike Marshall

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:59:45