Powershell Invoke-Sqlcmd不会捕获所有详细输出

时间:2019-01-28 15:08:11

标签: sql-server powershell powershell-v2.0 invoke-sqlcmd

我创建了以下powershell(v2)脚本来对目标数据库执行查询,并构建另一个要运行的查询:

    {
  "name": "mapvelo",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "dev": "webpack --mode development --config webpack.config.js --progress",
    "start": "webpack-dev-server -d --hot --config webpack.config.js --watch",
    "prod": "webpack --mode production --config webpack.config.js --progress"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.2.2",
    "@babel/plugin-proposal-private-methods": "^7.3.0",
    "@babel/preset-env": "^7.3.1",
    "@fortawesome/fontawesome-free": "^5.6.3",
    "babel-loader": "^8.0.5",
    "clean-webpack-plugin": "^1.0.1",
    "css-hot-loader": "^1.4.3",
    "css-loader": "^2.1.0",
    "csv-loader": "^3.0.2",
    "file-loader": "^3.0.1",
    "html-es6-template-loader": "^1.0.5",
    "html-webpack-plugin": "^3.2.0",
    "knacss": "^7.1.0",
    "mini-css-extract-plugin": "^0.5.0",
    "node-sass": "^4.11.0",
    "optimize-css-assets-webpack-plugin": "^5.0.1",
    "resolve-url-loader": "^3.0.0",
    "sass-loader": "^7.1.0",
    "standard": "^12.0.1",
    "style-loader": "^0.23.1",
    "typeface-roboto": "0.0.54",
    "typeface-zcool-qingke-huangyou": "0.0.71",
    "webpack": "^4.29.0",
    "webpack-cli": "^3.2.1",
    "webpack-dev-server": "^3.1.14",
    "xml-loader": "^1.2.1"
  },
  "dependencies": {
    "leaflet": "^1.4.0"
  }
}

在脚本末尾,我将打印必须执行的查询,但是不幸的是,变量$ sqlOutputToRun仅包含大约3/4的预期输出。 我认为输出会因为大小而被削减,但是我不确定如何扩展它。

您知道缺少什么吗?

2 个答案:

答案 0 :(得分:1)

如果您不能没有光标,为什么不能做这样的事情?

$sql = "
DECLARE @Tables CURSOR;
DECLARE @TableName NVARCHAR(200);
DECLARE @Results TABLE (ResultQuery NVARCHAR(MAX)) 

BEGIN
    SET @Tables = CURSOR FOR
    SELECT TABLE_NAME
    FROM testdb.INFORMATION_SCHEMA.TABLES 
    WHERE TABLE_TYPE='BASE TABLE' 
    --AND TABLE_NAME LIKE 'x%'

    OPEN @Tables 
    FETCH NEXT FROM @Tables 
    INTO @TableName
    WHILE @@FETCH_STATUS = 0
    BEGIN
        INSERT INTO @Results (ResultQuery)
        SELECT 'SELECT TOP 1 * FROM test.dbo.' + @TableName

        -- PRINT 'SELECT TOP 1 * FROM test.dbo.' + @TableName
        FETCH NEXT FROM @Tables 
        INTO @TableName 
    END; 

    CLOSE @Tables ;
    DEALLOCATE @Tables;
END;
SELECT * FROM @Results;"
$sqlOutputToRun = Invoke-Sqlcmd -query $sql
$sqlOutputToRun

答案 1 :(得分:1)

一旦转储app.clickedImage = changeRGB(app.clickedImage,app.addR,app.addG,app.addB);语句以支持结果集,您也可以转储光标,并且只需运行一个简单的查询,如:

print

因此在Powershell中:

    select 'SELECT TOP (1) * FROM '+ quotename(table_catalog) + '.' + quotename(table_schema) + '.' + quotename(table_name)
    FROM INFORMATION_SCHEMA.TABLES 
    WHERE TABLE_TYPE='BASE TABLE' 
    AND TABLE_NAME LIKE 'x%'