You are viewing limited content. For full access, please sign in.

Question

Question

Forms: Delete from SQL

asked on January 25, 2016

Hello,

I need some help, for the image attached:

There are two sections, Existing User & New User

Existing User: User provides short name & Name is displayed from database. User selects Delete Yes or No

Once the user selects Yes, the complete row should be deleted from the database, please note that user can add more rows for the table & in return all rows should be deleted as every "short" he selects is a row.

In the database there is a column called Short.

New User: User provides all the values & selects Create Yes or No

When user selects yes, the record should be created in the database, all the Labels are exactly the name of columns. Again, user can add many rows & all of the selected should be created.

 

I have the tokens populated in WF, also I have made Conditional Parallel activities for both if the user selects Yes.

However, my problem is to configure Custom Query to delete the row or rows & to create row or rows....

 

Thanks in advance!!!

 

0 0

Answer

SELECTED ANSWER
replied on January 26, 2016

Try this:

INSERT INTO [ExternalTables].[dbo].[EmployeeImport]

([Team], [Short], [Name], [LastName], [FirstName], [Position], [Telephone], [eMail])

VALUES

(?, ?, ?, ?, ?, ?, ?, ?)

With all these values changed just to question marks as in Miruna's reply:

0 0

Replies

replied on January 26, 2016

Thanks Dan, very kind of you to take the time!!!

1 0
replied on January 25, 2016

Hi Sahil,

 

See if this example helps. Here I'm taking data from forms, determining they wish to delete a row from SQL, and forming the custom query.

 

 

Cheers,

Carl

0 0
replied on January 25, 2016

Hi Sahil,

To delete a row from a table you should just be able to use:

DELETE from [YourDatabase].[dbo].[YourTable] WHERE [YourColumn] = @shortNameToken

And to insert the new row...

INSERT INTO [YourDatabase].[dbo].[YourTable] (YourColumn1, YourColumn2) VALUES (@FormData1, @FormData2)

And just add more columns/values depending on the amount of data you need to insert.

Hope this helps! Dan

0 0
replied on January 25, 2016

Thanks Guys,

I am little scared to touch, so I would put the stuff here, maybe you can give me the correct values:

Database Name: ExternalTables

Table Name: dbo.EmployeeImport

 

For Existing User:

Column Name : Short (same the value is being queried)

 

For New User: The complete value is to be created as a Row

 

Again, the requirement is that all records should be deleted & created.

 

Thanks so much!

 

0 0
replied on January 25, 2016

So,

I wrote the query as:

INSERT 
INTO [ExternalTables]
WHERE [Team] = @Team AND [Short] = @Short AND [Name] = @Name AND [LastName] = @LN AND [FirstName] = @FN AND [Position] = @Position AND [Telephone] = @Telephone AND [eMail] = @eMail 

 

it gave me error:    Custom Query 2    ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'WHERE'.

 

0 0
replied on January 25, 2016

Hi Sahil,

Basically you have to declare the columns all in one go, then declare the values when using INSERT INTO rather than using the WHERE statement. So I think this is what you're looking for:

INSERT INTO [ExternalTables].[dbo].[EmployeeImport]
(Team, Short, Name, LastName, FirstName, Position, Telephone, eMail)
VALUES
(@Team, @Short, @Name, @LN, @FN, @Position, @Telephone, @eMail)

However this will only work for creating a new row, and not for updating an existing row.

0 0
replied on January 25, 2016

Thanks Dan,

Now I get: 

 

    25.01.2016 17:23:23    Custom Query 2    ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the scalar variable "@Team".

0 0
replied on January 25, 2016

ODBC data sources do not support named parameters. See help file. Your query needs to use ? for each parameter instead of the @Name syntax.

0 0
replied on January 25, 2016

So it should be ?team

0 0
replied on January 25, 2016

No, just question mark.

1 0
replied on January 25, 2016

I corrected in the Parameters by giving @ sign, but still same error.... :(

0 0
replied on January 26, 2016

Thanks Miruna, Thanks Dan!!!

 

Anyway the script can be written in a way that if the record exists, to update values?

Or deleting the record & recreating is the only way?

S

0 0
replied on January 26, 2016

To update existing rows you can use:

UPDATE [ExternalTables].[dbo].[EmployeeImport]

SET [Position] = ?

WHERE [Short] = ?

But you'd have to know if the value already existed in the database to know which statement to use, I'm sure there's probably a way to do this but I personally don't know how, sorry! frown

0 0
replied on January 26, 2016

Hi,

I think I have run into a serious issue, when I create or delete the WF only takes the first record???

 

As per the image above I have two tables, how can I make sure that all rows inserted take effect?

 

S

0 0
replied on January 26, 2016

There are several solutions to updating data if it exists or inserting if it doesn't. Try this Google search for example.

If you are using a table in your form, your workflow will have to iterate through the results with a For Each Row activity and insert them into SQL individually.

1 0
replied on January 26, 2016
0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.