如何使用POWERSHELL脚本获取此SQL输出

时间:2019-06-14 09:54:40

标签: sql powershell windows-7

我有一组SQL指令,并且想要使用Powershell脚本进行精确的输出。我对PS脚本和SQL一无所知。有人可以帮我怎么做吗?

\\\\\\\\\\\\ SQL CODE \\\\\\\\\\\\\\\\\\\\\\\\\\\

DECLARE @table_name SYSNAME
    SELECT @table_name = 'Godzilla.Jobs'

    DECLARE 
          @object_name SYSNAME
        , @object_id INT

    SELECT 
          @object_name = '[' + s.name + '].[' + o.name + ']'
        , @object_id = o.[object_id]
    FROM sys.objects o WITH (NOWAIT)
    JOIN sys.schemas s WITH (NOWAIT) ON o.[schema_id] = s.[schema_id]
    WHERE s.name + '.' + o.name = @table_name
        AND o.[type] = 'U'
        AND o.is_ms_shipped = 0


    Etc....

\\\\\\\\\\我想要的输出\\\\\\\\\\\\\\\\\\\\\\\\

        CREATE TABLE [Godzilla].[Jobs]
        (
                 [JobId] BIGINT NOT NULL
               , [MacAddressWifi] NVARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
               , [SerialNumber] NVARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
               , [TrpId] NVARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
               , [SniffsPackage] NVARCHAR(4000) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
               , [DecryptPackage] NVARCHAR(4000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
               , [StatusID] INT NOT NULL
               , [DateInsert] DATETIME NOT NULL
               , [DateUpdate] DATETIME NULL
               , [EstimatedTime] BIGINT NULL
               , [Priority] BIGINT NULL
        )

1 个答案:

答案 0 :(得分:1)

没有内置的“ PowerShell”方式来运行SQL查询。如果您安装了SQL Server tools,则将获得一个Invoke-SqlCmd cmdlet。

由于PowerShell是基于.NET构建的,因此可以使用ADO.NET API来运行查询。