Question: I want to save a video file in C:\ by incrementing the file name e.g. video001.avi video002.avi video003.avi etc. i want to do this in java. The program is on 

[Problem in java programming on windows7 (working well in windows xp)](https://stackoverflow.com/questions/2298101/problem-in-java-programing-on-windows7working-well-in-windows-xp)

How do i increment the file name so that it saves without replacing the older file.

Answer: 
you should make a query string and then run it by `exec()` function.

getting server name :

```
SELECT @@SERVERNAME

```

getting current db name :

```
SELECT DB_NAME() AS DataBaseName

```
If it happens that you need to use some sort of **variable** in your arguments(e.g. collect remote's server updates since yesterday):

```
DECLARE @yesterday NVARCHAR(20) = '2016-09-23 08:16:20';
DECLARE @sql NVARCHAR(MAX) = N'SELECT * FROM database.targetTable AS origin
                          WHERE origin.columnWithDateTime >'''+@yesterday+''';';
PRINT @sql;
EXEC(@sql) AT linkedServer

```

\_\_\_\_\_\_  

Where: 

> 
> *database.targetTable* : For some reason SSMS 2008 R2 returns error if you describe it as [database].[targetTable], and i don't know why that happens.  
>   
> 
>  **@yesterday**: Is the variable you want to insert (this case, a string containing datetime-like element)  
>   
> 
> **PRINT @sql**: Just to verify if the quotes are correctly placed.   
>   
> 
> **columnWithDateTime**: Should be a column with *datetime* format (e.g. "timestamp", or similar to the **@yesterday** variable format.
> 
> 
> 

"OPENQUERY does not accept variables for its arguments.": [See Here](https://msdn.microsoft.com/en-us/library/ms188427.aspx) (MSDN: OPENQUERY (Transact-SQL)).