Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is {0},{1},{2},{3} in the SQL query

Tags:

c#

sql

sql-server

I want to know what {0} or {1} mean in the query given below

public static int EnsureObjectHasAliases(string type, int id, int programId, string[] aliasList)
{
    var sb = new StringBuilder();
    if(aliasList!=null)
    for (int i = 0; i < aliasList.Length; i++)
    {
        string alias = aliasList[i];
        if (NormalizeString(ref alias))
        {
        if (sb.Length > 0)
        sb.Append(",");
        sb.Append(FormatStringForSql(alias));
        }
    }
    if (sb.Length > 0)
    {
        string aliases = sb.ToString();
        var c = (int)GetValue("SELECT count(*) FROM {0}Alias WHERE ProgramId = {1} AND {0}Id = {2} and Alias in ({3})", type, programId, id, aliases);
    if (c > 0)
       return id;

    if (aliasList != null)
    if (aliasList.Any(alias => AddObjectAlias(type, programId, alias, id) < 0))
        return -1;
    }
        return id;
    }

I have no idea what this query means:

SELECT count(*) FROM {0}Alias WHERE ProgramId = {1} AND {0}Id = {2} and Alias in ({3})

I want to know what {0} or {1} or {2} and {3} and ALIAS is in the query. I know that it is representing some table but how?

Is it something related to String.Format()?

EDIT:-

My GetValue() function is like this:

public static object GetValue(string sql, params object[] args)
{
    return GetValue(GlobalFactories.GetLogger().GetDefaultTransaction(), sql, args);
}
public static object GetValue(SqlConnection db, string sql, params object[] args)
        {
            return GetValue(GlobalFactories.GetLogger().GetDefaultTransaction(), db, sql, args);
        }

public static object GetValue(LogTransactionBase logTransaction,SqlConnection db,  string sql, params object[] args)
{
    if (string.IsNullOrEmpty(sql))
    return null;
    if (args != null && args.Length > 0)
        sql = string.Format(sql, args);
        LastQuery = sql;
        var needToDispose = false;
        if(db==null)
        {
        db = GetDB();
        needToDispose = true;
        }
    try
    {
    using (var cmd = db.CreateCommand())
    {
     cmd.CommandText = sql;
     cmd.CommandTimeout = TIME_OUT;
     var v = cmd.ExecuteScalar();
     if (v == DBNull.Value) v = null;
     return v;
    }
    }catch(Exception ex)
    {
     logException(logTransaction,sql,ex);
     throw;
    }
    finally
    {
    if (needToDispose)
    db.Dispose();
    }
    }
like image 843
Rahul Tripathi Avatar asked Dec 02 '25 08:12

Rahul Tripathi


2 Answers

Is it something related to String.Format()?

Probably. Those are string formatting placeholders. GetValue most likely generates the actual sql string in a manner similar to:

public object GetValue(string template, params object[] args)
{
    ...
    string sql = string.Format(template, args);
    ...
}
like image 121
D Stanley Avatar answered Dec 05 '25 00:12

D Stanley


It's related to String.Format(). {0}, {1}, {2} and {3} replaces by values from type, programId, id, aliases

like image 31
MikkaRin Avatar answered Dec 04 '25 23:12

MikkaRin