Is there a way to parameterize the path used by a copy into statement in Snowflake's SQL? I'd like to do something like, for example: copy into #temp_int_stage/test_$v from ..., where $v is a SQL variable.
you can define variable in snowsql. For that you have to enable variable substitution. Later you can use variable with & operator
!define val = yourvalue
!set variable_substitution=True
copy into #temp_int_stage/test_&val from ..
Best way to accomplish this is through a Snowflake Stored Procedure. You can't do it directly using a variable in the SQL Statement, though.
If you are using any programming interface to load files from stage to final target table you can do it. We are using python script to load our data from snowflake stage to target table. Where in copy command file path will be parameterized.
Else, you can use in snowflake stored procedure. I'm not sure, is there any way to execute directly.
Related
I currently have code in a script that, looks something like:
SELECT * FROM [linkedServerName].database1.dbo.tableA;
SELECT * FROM [linkedServerName].database1.dbo.tableB;
SELECT * FROM [linkedServerName].database1.dbo.tableC;
except that it's not as neat as this and in reality there are references scattered all over a very long script.
I will need to run this script on a number of different test environments where the name of the linkedServer will need to change.
Is there a way that I can store these in a single variable so that I only need to make 1 adjustment?
(I tried to use a Synonym but it seems that I can only do that at a table level, so I would need multiples of them which defeats the purpose of what I am trying to achieve.)
It is possible by using sqlcmd variables in database project
https://learn.microsoft.com/en-us/sql/ssdt/add-database-reference-dialog-box?view=sql-server-ver15#:~:text=a%20composite%20project-,In%20Solution%20Explorer%2C%20right%20click%20References%20and%20select%20Add%20Database,and%20paste%20it%20into%20your%20.
The DACPAC along with the cmdvariable can be used to achieve your requirement
I want to know if there are any provisions in SQL server where we can store the constants variables either in some file or else just like codeigniter's constants.php file from where we can define constant variables and whenever we need we can call the variable.
I want to use these feature as because i have multiple views, stored procedures which takes static values and whenever these values changes in database i can just change those values in constant and my job is done. I am ok to create any XML or other file and call it in SP,View wherever i needed.
Thank you in advance.
This is not possible at the server level but you can do this if you have all the views in a single .SQL file (if you want to, several batches in a single .SQL script). You will need to use SQLCMD mode in SSMS. To enable SQLCMD mode go to Query > SQLCMD Mode
Once you have SQLCMD mode enabled you can define constants/variables at the top of your SQL script and use them throughout the different views in your .SQL file.
To define the variables/constants
:setvar cons1 10
:setvar cons2 20
And to use them
$(cons1)
$(cons2)
A "constants file" is really a list of key/value pairs
const key = "value"
Translated into SQL, a list of key/value pairs is a table
Key varchar() [UK]
IntValue int null
StringValue nvarchar() null
etc. for the supported data types.
Reference the values using
SELECT [ValueColumn] FROM [SettingsTable] WHERE Key = '[Key]'
Since the result is a NULLable scalar, you can use this expression to read a "constant" value in a sub-SELECT.
If your constants are really really constant, you can also define a view as
CREATE VIEW V_SystemConstants AS
SELECT 42 AS TheAnswer
This seems ridiculously easy, but I can't find it anywhere...
I have a VERY simple sequence container with two tasks: Truncate a SQL table, and repopulate it from production. But this container will be repeated for about 50 tables. The container's name (entered manually) = the name of both the source and destination tables.
I have two variables:
"TableName" is entered manually.
"DelTable" is an expression that uses #[User::TableName] to generate a simple SQL statement.
I'm super-lazy and would like to use an expression to set "TableName" = the name of the current scope so I only have to enter it once.
Ideas???
THANK YOU!
if you are truncating all tables in a DB and replacing with exactly the same structure, how about this approach:
Execute SQL:
select table_name
from INFORMATION_SCHEMA.TABLES --Add a where to limit the tables to the ones you want
Save results to an object variable called TABLES
Add a for each loop:
Loop through ADO Object setting value to a string variable called table
Add Execute SQL to FE LOOP: truncate table ? and map parameter.
Add a 2nd Execute SQL statement:
INSERT INTO SERVER.DB.SCHEMA.?
select * from ?
Again map the parameters.
If you are having trouble mapping parameters set up variables and use them to create the SQL statements to run.
#TomPhillips is correct, I cannot unfortunately comment or make that answer useful. hence commenting here.
There's no easy quick fix to use a loop/automate unless all the 50 tables are same structure which is rare by any stretch of imagination.
BIML is the way to go if you are lazy :)
SSIS is not dynamic. Data Flows require fixed input and output at compile time, not runtime. You cannot simply change the table name and have it work.
If you have a list of 50 tables to do the same function on, you can use BIML to dynamically generate the SSIS package(s). But the DF itself cannot be dynamic.
In SSIS, I am feeding a XML file and using XSLT getting one resultant XML(with my required field) file.
Could I use the result of XML task to directly feed the SQL table.
While exploring in this regard..i got to know I can save the result of XML task to a variable and can use this variable to put the data in SQL table.
Not sure how to accomplish this or is it possible ?
If you have the resultant value contained within a varaible. Assuming it is just one value. You could use a Parameterised Execute SQL Task to accomplish this.
I have written a stored procedure which uses bulk insert to load data from a *.csv file.
It works fine when I mention the physical path.Now I want to use the same stored procedure in a SQL server project.Hence I have to mention the relative path.Is there anyway I can do this?
Thanks in advance.
Your best bet would be to either pass the root in as a parameter to the SP, or have a config table in your DB and set the root path there. Heck, you might as well just pass in the entire fully qualified name of the file if you can.