B
B
Boris the Animal2016-03-16 13:44:01
SQL
Boris the Animal, 2016-03-16 13:44:01

How to correctly set parameters in a request?

There is a connection to the database, the request is working, there is a table in that database, everything for connecting to the database and sending the request is configured correctly, as I understand it. But why can't I substitute the variable value into the query, what am I doing wrong?
added later:
I temporarily solved this problem like this: in the script I simply dynamically generate a request, I substitute all the parameters in it directly into the request text. That's how it worked.

private const string NotifyManagerTimestampName = "NotifyManagerTimestamp";
        private const string SqlNotifyManagerTimestamp = "SqlNotifyManagerTimestamp";

        private const string SqlNotifyManagerPattern = @"UPDATE DatetimeStamp SET STAMP = CAST({0} AS DATETIME2) WHERE NAME = 'SsisTestManager';";

        /// <summary>
        /// This method is called when this script task executes in the control flow.
        /// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
        /// To open Help, press F1.
        /// </summary>
        public void Main()
        {
            try
            {
                DateTime now = DateTime.Now;
                string nowParam = "'" + now.Year + now.Month.ToString("00") + now.Day.ToString("00") + "'";
                string result = string.Format(SqlNotifyManagerPattern, nowParam);

                Dts.Variables[NotifyManagerTimestampName].Value = now;
                Dts.Variables[SqlNotifyManagerTimestamp].Value = result;
                
                Dts.TaskResult = (int)ScriptResults.Success;
            }
            catch (Exception)
            {
                Dts.TaskResult = (int)ScriptResults.Failure;
            }
        }

the end.
The query I use in the "Execute SQL Task"
UPDATE DatetimeStamp SET STAMP = CAST([NotifyManagerTimestamp] AS DATETIME2) 
WHERE NAME = 'SsisTestManager';

938126a0c2be42c3852e21cccb51b883.jpg1ad550f2338f430f984e2b3186ab3391.jpgbe86fe1e5cac47d68405c9806959935c.jpg
- Task Execute SQL task:
Data
validation started Data validation completed
[Execute SQL task] Error: Executing the query "UPDATE DatetimeStamp SET STAMP = CAST([NotifyManag..." failed with the following error: "Invalid column name 'NotifyManagerTimestamp'.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Task 'Execute SQL' task failed

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Eremin, 2016-03-16
@EreminD

1) Why write in []?
2) Please show how your SQL query is formed and called

A
Artyom Karetnikov, 2016-03-18
@art_karetnikov

The fact that you are making a request means the following:
For the STAMP field,
take the value from the [NotifyManagerTimestamp] field
Therefore, the error. They write to you about it: such a field was not found. If you want to use a variable, then it's like this: @NotifyManagerTimestamp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question