kusto command to create a table within highlighted database - database

I want to write a query to select a DB and then create a table within that DB.
I have the following query to just create a table, Is there a way to highlight the DB within the command itself?
.create table ABC (
A:string ,
B:string
)

Is there a way to highlight the DB within the command itself?
No - as the command runs in the context of a specific database, you must first make sure you're in the context of the database in which you wish to create the table.
In the web interface, that means 'selecting' the database in the connections panel.
In the API, that means explicitly specifying the database name in the connection string builder (or as part of the REST request parameters), or when calling the method to execute the control command.

Related

Export data to a text file from a SQL Server table with changing columns

I have created a Pivot table using columns passed in from a variable. This variable is set based on a flag in another table, so the name and number of columns can change month-to-month. I need to be able to export this pivot to a delimited text file. The pivot table is stored in a temp table ##PivotTable
I have tried to do this via SSMS, but the only way I know how to do this is to define a file connection manager, which needs set columns. I also tried via sqlcmd, but that one exports extra information and spaces.
I am open to pretty much any process that would allow me to run this via a SQL Agent job on a monthly basis.
Any assistance would be much appreciated.
You can use SQL server import and export wizard
Right click on the database-->Tasks-->ExportData..
map your ##PivotTable.
Click next and set your source database (Select OLEDB provider for SQL Server)
Set a file name for your file (e.g. d:\monthlyData\202006\MyFile.csv
Select option copy data from one to more tables or views -- do not use this option due the fact that your columns getting changed.
Select option write a quarry
You can run this immediately or save as SSIS package and reuse it using SQL job
I think you'll have to use dynamic SQL like found here.

SSIS Logging detail from SP with Full Result Set

(I am not a vested enough member to include screen shots in my post - here is a link to a shared OneDrive folder which has article with images to explain better https://1drv.ms/f/s!Ai9bJ2dV87SLg9h5FP84If74hyUK4Q)
I am trying to log what particular stored procs have inserted, updated and deleted after they were executed via Execute SQL task within a SSIS package workflow. There was a custom logging method that a 3rd party implemented, but it worked by relating a System ParentContainer ID to a user Task ID which served as a parameter to a stored procedure which logged such information. 1) I don’t think this will work from an Execute SQL Task and 2) I want a level of detail that extends past what DML function occurred.
I have been successful in logging a “single row” by setting up a result set, using variables and via an adjacent Data Flow task using a derived column task to retrieve the variables and insert into a log table.
As an example of my current working method:
The Exec SQL Task setup
Detail of the data flow part that logs
I am now coming across stored procedures that perform multiple inserts, thus I have the need to log the additional detail - more than one row. I created variables in the proc to retrieves this multiple INSERT scenario and have a union select in the SP that yields the following result set.
I understand that I now need to use the Full Result Set setting now but for this application what method is used for to persist the result set to another step (for me a destination in order to log). Amongst research I understand how one may use a Foreach loop container but this appears to configurable to one variable which needs to be value type object. I currently have 4 variables here and unable to setup as such in the Collection section of the Foreach object.
I would appreciate any insight in achieving this or suggestion to another method altogether.
many thanks in advance!
Your INSERT_B_BUDGET SQL task generates more than 1 row, you want to use Foreach Loop to catch the full result set, correct?
create a variable, LoopObject, data type as Object.
edit INSERT_B_BUDGET task,
in General tab, change ResultSet to Full result set
in Result Set tab, Variable Name as LoopObject.
add Foreach loop container after INSERT_B_BUDGET task, move your Logging 1 task into the container.
in Collection tab, Enumerator, select Foreach ADO Enumerator, ADO object source variable as LoopObject, Enumeration mode, select Rows in the first table.
in Variable Mappings, add your existing 4 variables.
You have to set up a profiler trace to track what procs are being executed. You should set up filters on the trace by database and user and use the SQLProfilerTSQL_SPs template.

How to copy a single view with all the data on it in sql server from one database to another database?

I am looking for some idea if we can generate a script for just one view and run that on the another database to create that view with its datas intact. Please help, thank you
If your destination server is not linked with the source, getting this data out will take a few more steps. I am assuming that you only want to transport the data from the view, but the steps below could be applied to the source table(s), making this view instantiation part unnecessary.
First, since a view does not store data, it only references data, you will need to instantiate the view into a table.
Select *
INTO tblNewTable --this creates a new table from the data selected from the view
FROM dbTest.dbo.Tester;
Next, open SSMS. Right click the database, select tasks, then generate scripts
Then select the newly created table, and next
You will need to select advanced and change the 'types of data to script' to schema and data. It will be schema only by default. Select Next and Finish.
SSMS will export a file, or load a new query window with the code to create a new table, but will also have the insert statements to load the new table exactly as it was on the source server
Use following as an example
use dbNew;
go
create view dbo.ViewTest as
select * from dbTest.dbo.Tester;
Following code will create table using another table. The new Table will contain all the data of the previous table.
Select * into DBName1.SchemaName.NewTableName from DBName2.SchemaName.PreviousTableName
You can use this query to create new table in any database and schema.

Entity Framework and SQL Server OUTPUT clause

I'd like to use SQL OUTPUT clause to keep history of the records on my database while I'm using Entity Framework. To achieve this, EF needs to generate the following example for a DELETE statement.
Delete From table1
output deleted.*, 'user name', getdate() into table1_hist
Where field = 1234;
The table table1_hist has the same columns as table1, with the addition of two columns to store the name of the user who did the action and when it happened. However, EF doesn't seem to have a way to support this SQL Server's clause, so I'm lost on how to implement that.
I looked at EF's source code, and the DELETE command is create inside a internal static method (GenerateDeleteSql in System.Data.Entity.SqlServer.SqlGen.DmlSqlGenerator class), so I can't extend the class to add the behavior I want. It looks like I'll have to rewrite the SQL Server provider based on the existing code, but that is something I'd like to avoid...
So, my question is if there's another option to do this (an extension, for example) or do I have to rewrite this provider?
Thank you.
Have you considered either
Using Stored Procedures to encapsulate your data logic
A delete trigger to capture the data
Change Data Capture (Enterprise edition only)
not actually deleting the data - merely setting a flag in the data to mark it as deleted.

Need Help to set up Package that contains Foreach Loop container SSIS

I am new with SSIS packages, There is a specific process I want to accomplish.
1st) I want to get a record set from a SQL Server DB and Mark that I have retrieved the specific rows.
2nd) Loop through each row
3rd) for each row looped through call a web service and pass two fields to the web service as input parameters.
Any help will be much appreciated.
The first step to do this is to create a few SSIS variables. Select View > Other Windows > Variables to display the variables window. Add a variable that will contain your result set from the SQL query. Set the data type to Object. Create additional variables for each column selected in the query.
In the Connection Managers tab, add a new Connection Manger. Edit and configure the new Connection Manager with the SQL Server connection information (e.g., Server name, authentication and database, etc.)
Add an Execute SQL Task to the Control Flow. Edit the task. In the Connection property, select your new Connection Manager. Enter the SQL SELECT statement in the SQLStatement property. Set the ResultSet property to Full result set.
Select the Result Set tab in the Execute SQL Task. Add a new result set. Set the Result Name to 0 and the Variable Name to the object variable you created earlier. Close the editor.
Add a Foreach Loop Container to Control Flow. Create a Precedence Constraint (green line) from the Execute SQL Task to the Foreach Loop Container. This will ensure that the SQL task must succeed before the Foreach Loop starts.
Edit the Forach Loop Container and select the Collection tab. Set the Enumerator property to Foreach ADO Enumerator and select your object variable in the ADO object source variable. Select the Variable Mappings tab. Select your variables that will retrieve the column value for each row. Associate the first variable with the first column by selecting index 0. Index 1 will be the second column, etc.
Add a Web Service Task in the Foreach Loop Container. This task will run once for each row in the result set. You can use the variable values to configure the Web Service Task. You may need to work with expressions if the property doesn't directly accept variables.
If you want to mark your rows one at a time, you can add another Execute SQL Task and place it into the Foreach Loop Container. Configure it similarly to the first Execute SQL Task, but create an SQL UPDATE statement. You might consider updating all rows the rows at once to get better performance. If so, then move the Execute SQL Task out of the Foreach Loop Container and configure the UPDATE statement so that it updates the set of rows.

Resources